الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

مساعدة في Dll

بدأه elgoeg في 17 أبريل 2008 · 2 رد · 647 مشاهدة · في Microsoft Visual C#.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

لقد قمت بعمل ملف dll بلغة الC# وهو يحوي على class Form واريد انا استخدم هذا الclass بمشروع اخر على ان يتم اضافته بشكل برمجي وليس add reference

فارجو المساعدة

#2

Reflection

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");

DynamicLoadDll.zip

تم تعديل هذه المشاركة بواسطة Moudey في 18 أبريل 2008 في 03:45

مواضيع متنوعة

#3

السلام عليكم

قمت باضافة المكتبة للمشروع عبر

ثم داخل الكود قم

Class1 C1 = new C1();
c1.YourMethodName();
.....

لا تنسى تعريف القسم الموجود داخل المكتبة ب Public

مواضيع مشابهة