السلام عليكم
لقد قمت بعمل ملف dll بلغة الC# وهو يحوي على class Form واريد انا استخدم هذا الclass بمشروع اخر على ان يتم اضافته بشكل برمجي وليس add reference
فارجو المساعدة
السلام عليكم
لقد قمت بعمل ملف dll بلغة الC# وهو يحوي على class Form واريد انا استخدم هذا الclass بمشروع اخر على ان يتم اضافته بشكل برمجي وليس add reference
فارجو المساعدة
Dynamically load a class and execute a method in .NET
لقد قمت بكتابة هذا الكلاس بعد مراجعة وفهم المصادر السابقة.
public class DynamicAssembly
{
public static object Load(string assemblyPath, string className, string methodName, params object[] parameters)
{
System.Reflection.Assembly asm;
System.Type type;
System.Reflection.MethodInfo methodInfo;
try {
asm = System.Reflection.Assembly.LoadFile(System.IO.Path.GetFullPath(assemblyPath));
// Get the type of class.
type = asm.GetType(className);
if(type == null)
throw new Exception(string.Format("Class name \"{0}\" not found", className));
if(parameters == null || parameters.Length == 0) {
// Get the method information for function.
methodInfo = type.GetMethod(methodName, new Type[] { });
if(methodInfo == null)
throw new Exception(string.Format("Method name \"{0}\" not found", methodName));
return methodInfo.Invoke(null, null);
}
Type[] typeOfParameters = new Type[parameters.Length];
for(int i = 0; i < typeOfParameters.Length; i++) {
typeOfParameters = parameters.GetType();
}
// Get the method information for function.
methodInfo = type.GetMethod(methodName, typeOfParameters);
if(methodInfo == null)
throw new Exception(string.Format("Method name \"{0}\" not found", methodName));
if(parameters.Length > 0) {
if(parameters.Length != methodInfo.GetParameters().Length) {
// Method in call
string strParameters = "Error " + methodName + "(";
foreach(object param in parameters) {
strParameters += param.GetType().Name + ", ";
}
strParameters = strParameters.Remove(strParameters.Length - 2) + ")";
// MethodInfo in dll
strParameters += "\r\nMethod Must be equals\r\n" + methodName + "(";
foreach(System.Reflection.ParameterInfo param in methodInfo.GetParameters()) {
strParameters += param.ParameterType.Name + ", ";
}
strParameters = strParameters.Remove(strParameters.Length - 2) + ")";
throw new Exception(strParameters);
}
return methodInfo.Invoke(null, parameters);
}
} catch(Exception ex) {
throw ex;
} finally {
asm = null;
type = null;
methodInfo = null;
}
return null;
}
}MessageBox.Show(DynamicAssembly.Load("TestDll.dll", "TestDll.Class1", "Sum", 10).ToString());
MessageBox.Show(DynamicAssembly.Load("TestDll.dll", "TestDll.Class1", "Sum", 10, 10).ToString());
MessageBox.Show(DynamicAssembly.Load("TestDll.dll", "TestDll.Class1", "Sum", 10, 10, 10).ToString());
DynamicAssembly.Load("TestDll.dll", "TestDll.Class1", "ArabTeam2000");
DynamicAssembly.Load("TestDll.dll", "TestDll.Class1", "ArabTeam2000", "My account is 'Moudey' in ArabTeam2000");تم تعديل هذه المشاركة بواسطة Moudey في 18 أبريل 2008 في 03:45