Saturday, 15 March 2014

c# - Reflection-generated and generic types -


me reflection. There is one more bad moment with amit. and type management.

Say, I have a name with the name "code> MyType , which is defined in the dynamic generated assembly. MyType.GetMethods () results in NotSupportedException , which has reduced me to write my own wrapper and lookup tables, however, this is happening when I type the GetMethods () or any other introspection methods that are used as general arguments They use their own types:

  • tuples , => work fine
  • Tupal myType> = & gt; Exceptions

    I can get a list of methods by definition of a common type:

    Typef (Tuplé & lt; int, MyType) .GetGenericTypeDefinition (). GetMethods ()

    However, the actual parameters of methods (like T1 , TResult etc), and I do not think write another writing. Like, which withdraws the usual arguments in its original values.

    A sample code:

      var asmName = new email match ("Test"); Var access = AssemblyBuilderAccess.Run; Var asm = AppDomain.CurrentDomain.DefineDynamicAssembly (asmName, access); Var module = asm.DefineDynamicModule ("Test"); Var aType = module.DefineType ("A"); Var Tuplettype = Typef (Tupal ;, & gt;); Var Tupal = Tuliptype Mechanical type (new [] {typef (int, atip}); Tuple.GetProperty ("item 1"); // & lt; - There is an error   

    So the questions are:

    1. How can I find that one type is safe to call GetMethods () and similar ways?
    2. If the type is not secure then how can I get the actual list of methods and their common logic values?

      I got an answer in one. There is a bunch of stable overloads in the TypeBuilder class that works at all:

        var genericTuple = typeof (tupl ;, & gt;); Var myTuple = Generic Tupale.Magener Type (Typef, MyType); Var ctor = TypeBuilder.GetConstructor (myTuple, genericTuple.GetConstructors (.) First ());   

      Surprisingly, there is no overload of GetProperty . However, property recipients and setters can still be solved using GetMethod :

        var genericGetter = typeof (Tuple & lt ;, & gt;). GetProperty ("Item1"). GetMethod; Var real = typebilder GetMeath (myTuple, GenericGuiter);    

No comments:

Post a Comment