2010-01-31 3 views
9

런타임시 .NET DLL을로드 할 수 있는지, 사용 가능한 방법을보고 런타임에서 실행할 수 있는지 궁금합니다. 이 가능한 경우런타임시 호출 방법

당신이 올바른 방향으로 당신은 Reflection를 사용할 필요가

답변

3

날 지점 수 있습니다.

당신은 DLL의 클래스를보고 반환 Assembly 개체의 GetTypes 방법을 다음 닷넷 어셈블리를 포함하는 .DLL로드 전화 Assembly.LoadFile를 호출 할 수 있습니다.
원하는 클래스에 대해 Type 개체를 찾았 으면 InvokeMember 메서드를 호출하여 함수를 호출 할 수 있습니다.

반사가 매우 느릴 수 있으므로주의하십시오.

+0

당신은 반사를 통해 메소드를 호출의 성능을 향상시킬 수 있습니다 사용'Delegate.CreateDelegate (...)'대신 얻어서 메서드를 호출하고 순수하게 리플렉션으로 호출 : http://msdn.microsoft.com/en-us/library/system.delegate.createdelegate.aspx –

+1

@ Dan : True. 그러나 컴파일 타임에 서명을 알고있는 경우에만 가능합니다. – SLaks

+0

또는 DynamicMethod를 사용하십시오. – codekaizen

1

나는 에서 이걸 발견 reflection eamples

// get all public static methods of MyClass type 
    MethodInfo[] methodInfos = typeof(MyClass).GetMethods(BindingFlags.Public | 
                BindingFlags.Static); 


[C#] 
// dynamically load assembly from file Test.dll 
Assembly testAssembly = Assembly.LoadFile(@"c:\Test.dll"); 

[C#] 
// get type of class Calculator from just loaded assembly 
Type calcType = testAssembly.GetType("Test.Calculator"); 

[C#] 
// create instance of class Calculator 
object calcInstance = Activator.CreateInstance(calcType); 

[C#] 
// get info about property: public double Number 
PropertyInfo numberPropertyInfo = calcType.GetProperty("Number"); 

[C#] 
// get value of property: public double Number 
double value = (double)numberPropertyInfo.GetValue(calcInstance, null); 

[C#] 
// set value of property: public double Number 
numberPropertyInfo.SetValue(calcInstance, 10.0, null); 

[C#] 
// get info about static property: public static double Pi 
PropertyInfo piPropertyInfo = calcType.GetProperty("Pi"); 

[C#] 
// get value of static property: public static double Pi 
double piValue = (double)piPropertyInfo.GetValue(null, null); 

[C#] 
// invoke public instance method: public void Clear() 
calcType.InvokeMember("Clear", 
BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public, 
null, calcInstance, null); 

[C#] 
// invoke private instance method: private void DoClear() 
calcType.InvokeMember("DoClear", 
BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.NonPublic, 
null, calcInstance, null); 

[C#] 
// invoke public instance method: public double Add(double number) 
double value = (double)calcType.InvokeMember("Add", 
BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public, 
null, calcInstance, new object[] { 20.0 }); 

[C#] 
// invoke public static method: public static double GetPi() 
double piValue = (double)calcType.InvokeMember("GetPi", 
BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public, 
null, null, null); 

[C#] 
// get value of private field: private double _number 
double value = (double)calcType.InvokeMember("_number", 
BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic, 
null, calcInstance, null); 
3

예,이, 당신은 당신의 DLL로드로 시작 가능하다 :

Assembly assembly = Assembly.LoadFrom(assemblyPath); 

을 그리고 당신이거야 당신의 DLL 내부의 메소드를 호출 reflection을 사용해야합니다.

object obj = assembly.CreateInstance(<type.FullName>); 

여기서 type.FullName은 해당 어셈블리의 일부 유형의 FullName 속성입니다. 당신이 당신의 인스턴스를 가지고 일단

,이처럼 메소드를 호출 할 수 있습니다 :

MethodInfo methodInfo = obj.GetMethod("MyMethod"); 
methodInfo.Invoke(obj,null); 
관련 문제