방출 할 때 유형이 미완료 인 대리인에게 전화를 발신하는 데 문제가 있습니다. 에 *방출 할 때 형식이 완료되지 않은 대리자를 호출하려면 어떻게해야합니까?
public MyClass {
// Array of delegates. T has been replaced with MyClass because the
// argument will be 'this', which is of type MyClass.
private static DirectReadAccessor<MyClass>[] directReadAccessors;
// Method that looks up a delegate in the array of delegates and calls it
// with 'this'.
public Object DirectRead(int i) {
directReadAccessors[i](this);
}
// Method that is called by the declaring type to pass an array with the
// MethodInfo of some methods. MyClass then creates delegates for these
// methods and stores them in the directReadAccessors array.
public static void InitializeClass(MethodInfo[] directReadAccessorsMInfo) {
int length = directReadAccessorsMInfo.Length;
Type[] typeArguments = new Type[] { typeof(MyClass) };
directReadAccessors = new DirectReadAccessor<MyClass>[length];
// For each method in directReadAccessorsMInfo...
for (int i = 0; i < length; i++) {
// Create a delegate and store it in directReadAccessors.
directReadAccessors[i] = (DirectReadAccessor<MyClass>)
Delegate.CreateDelegate(
DirectReadAccessor<MyClass>, // Type of the delegate.
null, // Specify null first argument so that it's
// *open* instance.
directReadAccessorsMInfo[i].MakeGenericMethod(typeArguments) // The method.
);
}
}
}
: 만든 다음 클래스를
// Delegate type. The 'firstArgument' will be 'this', i.e., this is an open
// instance method: the implicit argument is here given explicitly, in
// 'firstArgument'. (See link below for explanation on open instance delegates).
public delegate Object DirectReadAccessor<T>(T firstArgument);
그리고 지금의 (a하는 TypeBuilder으로, 즉) 동적에 노력하고있어 : 내가 선언 한 다음 대리자 형식을 : 나는 정교한 것이다 open instance delegates.
DirectReadAccessor [] 유형의 필드 인 directReadAccessors를 선언하려고하거나 MyClass를 다시 사용하는 InitalizeClass 메소드를 내보낼 때 MyClass가 존재하지 않기 때문에 까다로운 작업이었습니다. 아직 존재합니다 (내가 작성한 것입니다). 그러나, 나는이 모든 것을 할 수 있었지만, 이제는 DirectRead 메소드에 문제가있다. 일단 스택에 가지고 있다면 위임자를 호출하는 방법을 모른다. 내가 그렇게 같이 받아야
ilGenerator.Emit(OpCodes.Callvirt, invokeMInfo);
invokeMInfo이 DirectReadAccessor에서의 메소드 호출이며, 어떤 : : 분명히 내가 필요한 것은 다음과 같은 두 개의 발광입니다
MethodInfo invokeMInfo = typeof(DirectReadAccessor<MyClass>).GetMethod(
"Invoke", // Name of the method.
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, // Binding attributes.
null, // Binder.
new Type[] { typeof(MyClass) }, // Types of the arguments.
null // Modifiers for the arguments.
);
다시, 문제는 그 MyClass의도 DirectReadAccessor도 아직 존재하지 않습니다. 나는 위의 그림과 같이 directReadAccessorType에 (, ...를 "호출")
directReadAccessorType = typeof(DirectReadAccessor<>).MakeGenericType(typeBuilder);
하지만 getMethod 메소드를 호출하려고하면 : 나는 이렇게 만든 MyClass에와 미완성 DirectReadAccessor 유형의하는 TypeBuilder을 가지고 미완성 된 형식에 대한 메서드 호출을 얻을 수 없기 때문에 NotSupportedException을 가져옵니다. 필자는이 가정을 다음과 같이 유형을 마무리 한 후에 동일한 호출을 수행하여 테스트했습니다.
typeBuilder.CreateType();
실제로 예외는 없습니다. 그러나 InitializeClass에 대한 코드를 내보내는 동안 형식을 완료하기 전에 Invoke 메서드의 MethodInfo를 가져올 수 있어야합니다.
이상한 상황입니다. 필요할 때 위임자를 보내 겠지만 위임 코드를 생성 할 수는 없습니다. 아무도 도움을 줄 수 있습니까?
감사합니다. 오랜 기간 동안 죄송합니다.
아니 범죄의 라인을 따라 전화를 사용하여이
MethodInfo
당신이 필요로 얻을 수TypeBuilder.GetMethod
를 사용 할 수 있어야한다고 생각하지만, 그것은 단지 나, 또는 DEVS는 너무 복잡 코드를 작성합니까?! ? –촬영되지 않았습니다.이 프로젝트는 5 개월짜리 프로젝트로 시작하기에 꽤 복잡한 기존 시스템을 C#으로 이식하려고 시도합니다. 현실에서 내가하려고하는 것은 그다지 복잡하지 않다. 단지 대리자 배열을 가지고 있고, 호출 할 수 있기를 원한다. 유일한 문제는 내가 동적으로 생성 된 유형을 사용하고 있지만 그러한 유형을 사용할 수 없다면 먼저 그 기능을 제공하는 것이 무엇입니까? – Alix