У меня проблема с Reflection.Emit. Я хочу иметь динамически создаваемый класс с простой реализацией ICollection. Все методы, которые я определил нормально, вместо следующих двух: общедоступный IEnumerator GetEnumerator () и IEnumerator IEnumerable.GetEnumerator ()
Следующий код показывает, что я хочу быть в моем динамическом классе:
public class SomeClassThatIsIEnumerable<T> : IEnumerable<T>
{
public IEnumerator<T> GetEnumerator()
{...}
IEnumerator IEnumerable.GetEnumerator()
{...}
}
Этот вывод с отражателя открыл мою динамическую сборку:
public class SomeClassThatIsIEnumerable<T> : IEnumerable<T>
{
public IEnumerator<T> GetEnumerator()
{
...
}
IEnumerator GetEnumerator()
{
...
}
}
Я определяю свой класс следующим образом:
TypeBuilder myType = module.DefineType("myType"...);
myType.AddInterfaceImplementation(typeof(IEnumerable));
myType.AddInterfaceImplementation(typeof(IEnumerable<T>));
myType.AddInterfaceImplementation(typeof(ICollection<T>));
myType.DefineMethodOverride(myDefineGetEnumerator(...),typeof(IEnumerable).GetMethod("GetEnumerator");
myType.DefineMethodOverride(myDefineGetGenericEnumerator(...),typeof(IEnumerable<T>).GetMethod("GetEnumerator);
//Definitions of other ICollection methods
//Define GetEnumerator is looks like this:
MethodBuilder method = myType.DefineMethod("GetEnumerator", MethodAttributes.Final | MethodAttributes.Virtual...)
ILGenerator il = method.GetILGenerator();
// adding opcodes
когда я вызываю myType.CreateType, исключение TypeLoadException с сообщением метод GetEnumerator не имеет реализации. Я предлагаю на проблему с методом IEnumerable.GetEnumerator, потому что у меня были проблемы при написании его на C #, даже в IL :). Может кто-нибудь мне помочь?