System.Reflection.AssemblyBuilder: adding referenced assemblys

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Sander

    System.Reflection.AssemblyBuilder: adding referenced assemblys

    Hello ng,
    anyone knowns how to add a reference to an assembly to
    System.Reflecti on.AssemblyBuil der?

    In System.Web.Comp ilation.Assembl yBuilder is a function like
    AddAssemblyRefe rence, but not in System.Reflecti on.

    Regards,
    Michael


  • Marc Gravell

    #2
    Re: System.Reflecti on.AssemblyBuil der: adding referenced assemblys

    Didn't you ask this a few hours ago?

    Comment

    • Michael Sander

      #3
      Re: System.Reflecti on.AssemblyBuil der: adding referenced assemblys

      yeah, but it was deleted somehow
      "Marc Gravell" <marc.gravell@g mail.comschrieb im Newsbeitrag
      news:OQ8LKS%23E JHA.4712@TK2MSF TNGP02.phx.gbl. ..
      Didn't you ask this a few hours ago?

      Comment

      • Marc Gravell

        #4
        Re: System.Reflecti on.AssemblyBuil der: adding referenced assemblys

        Nope - still there... I get it both from news.microsoft. com and from
        google groups (watch for wrap...)



        Marc

        Comment

        • Michael Sander

          #5
          Re: System.Reflecti on.AssemblyBuil der: adding referenced assemblys

          oh, just found your answer. funny, my orginal entry has been deleted, but
          your answer ist there.

          yes, i need to i guess. the problem is, that i create an interface there,
          that inherits from some other interface.

          if both interfaces a created in the dynamic assembly all works fine.
          if the base interface is created in some other assembly it wont show up in
          the class viewer as base type of my dynamic interface.
          even more weird: i can create an object of the type of the dynamic
          interface, query the implemented interfaces an it shows the dynamic
          interface. but the cast fails.


          "Marc Gravell" <marc.gravell@g mail.comschrieb im Newsbeitrag
          news:OQ8LKS%23E JHA.4712@TK2MSF TNGP02.phx.gbl. ..
          Didn't you ask this a few hours ago?

          Comment

          • Marc Gravell

            #6
            Re: System.Reflecti on.AssemblyBuil der: adding referenced assemblys

            OK, here's a complete example that declares an interface in one
            dynamic assembly, implements it in another, and then tests the
            interface via reflection. It also saves both assemblies to disk so you
            can load them in reflector/ildasm and verify that the references have
            been set correctly (which they have, for me at least...):

            using System;
            using System.Reflecti on;
            using System.Reflecti on.Emit;

            namespace ConsoleApplicat ion3
            {
            class Program
            {
            static void Main()
            {
            AssemblyBuilder assembly =
            AppDomain.Curre ntDomain.Define DynamicAssembly (
            new AssemblyName("F oo"),
            AssemblyBuilder Access.RunAndSa ve);
            ModuleBuilder module =
            assembly.Define DynamicModule(" Foo.dll");
            TypeBuilder tb = module.DefineTy pe("IFoo",
            TypeAttributes. Interface | TypeAttributes. Abstract |
            TypeAttributes. Public);
            PropertyBuilder prop = tb.DefineProper ty("Message",
            PropertyAttribu tes.None, typeof(string), Type.EmptyTypes );
            MethodBuilder method = tb.DefineMethod ("get_Messag e",
            MethodAttribute s.Public | MethodAttribute s.HideBySig
            | MethodAttribute s.NewSlot | MethodAttribute s.Abstract
            | MethodAttribute s.Virtual, CallingConventi ons.HasThis,
            typeof(string), Type.EmptyTypes );
            prop.SetGetMeth od(method);
            Type ifoo = tb.CreateType() ;
            assembly.Save(" Foo.dll");


            assembly = AppDomain.Curre ntDomain.Define DynamicAssembly (
            new AssemblyName("B ar"),
            AssemblyBuilder Access.RunAndSa ve);
            module = assembly.Define DynamicModule(" Bar.dll");
            tb = module.DefineTy pe("Bar", TypeAttributes. Class |
            TypeAttributes. Sealed | TypeAttributes. Public);
            tb.DefineDefaul tConstructor(Me thodAttributes. Public);
            tb.AddInterface Implementation( ifoo);
            MethodInfo interfaceMethod =
            ifoo.GetMethod( "get_Messag e");
            method = tb.DefineMethod ("MyMethodImpl" ,
            MethodAttribute s.Private | MethodAttribute s.HideBySig
            | MethodAttribute s.NewSlot | MethodAttribute s.Final |
            MethodAttribute s.Virtual | MethodAttribute s.SpecialName
            , CallingConventi ons.HasThis, typeof(string),
            Type.EmptyTypes );
            ILGenerator il = method.GetILGen erator();
            il.Emit(OpCodes .Ldstr, "hello world");
            il.Emit(OpCodes .Ret);
            tb.DefineMethod Override(method , interfaceMethod );
            Type bar = tb.CreateType() ;
            assembly.Save(" Bar.dll");

            typeof(Program) .GetMethod("Tes t").MakeGeneric Method(ifoo,
            bar).Invoke(nul l, null);
            }

            public static void Test<TInterface , TClass>()
            where TInterface : class
            where TClass : class, TInterface, new()
            {
            // create an instance
            TClass obj = new TClass();

            // check the cast (already validated by generic
            constraint)
            TInterface i = obj;

            // try to read the property
            PropertyInfo prop =
            typeof(TInterfa ce).GetProperty ("Message");
            object val = prop.GetValue(o bj, null);
            Console.WriteLi ne(val);
            }
            }
            }

            Comment

            • Marc Gravell

              #7
              Re: System.Reflecti on.AssemblyBuil der: adding referenced assemblys

              My main thoughts: are you calling AddInterfaceImp lementation /
              DefineMethodOve rride? These are the important bits for applying the
              interface implementation.

              Also - sorry if the code (previous post) is ugly; it largely goes with
              the turf for Reflection.Emit , I'm afraid...

              Marc

              Comment

              • Michael Sander

                #8
                Re: System.Reflecti on.AssemblyBuil der: adding referenced assemblys

                hi marc,
                i adjusted your example to meet my case. the cast works fine, there might be
                some other issue in my code, but still i cant see IFoo/Foo as BaseType in
                the ClassViewer.

                using System;

                using System.Reflecti on;

                using System.Reflecti on.Emit;

                namespace ConsoleApplicat ion3

                {

                public interface IFoo

                {

                }

                public class Foo : IFoo

                {

                }

                class Program

                {

                static void Main()

                {

                AssemblyBuilder assembly = AppDomain.Curre ntDomain.Define DynamicAssembly (new
                AssemblyName("B ar"), AssemblyBuilder Access.RunAndSa ve);

                ModuleBuilder module = assembly.Define DynamicModule(" Bar.dll");

                TypeBuilder tb = module.DefineTy pe("IBar", TypeAttributes. Interface |
                TypeAttributes. Abstract | TypeAttributes. Public);

                tb.AddInterface Implementation( typeof(IFoo));

                Type iBar = tb.CreateType() ;

                tb = module.DefineTy pe("Bar", TypeAttributes. Public | TypeAttributes. Class,
                typeof(Foo));

                tb.AddInterface Implementation( iBar);

                Type bar = tb.CreateType() ;

                assembly.Save(" Bar.dll");

                typeof(Program) .GetMethod("Tes t").MakeGeneric Method(iBar, bar).Invoke(nul l,
                null);

                }

                public static void Test<TInterface , TClass>() where TInterface : class where
                TClass : class, TInterface, new()

                {

                TClass obj = new TClass();

                TInterface i = obj;

                }

                }

                }


                Comment

                • Marc Gravell

                  #9
                  Re: System.Reflecti on.AssemblyBuil der: adding referenced assemblys

                  I tried your code; it looks fine in reflector, so I imagine that VS is
                  just having a hard time resolving the interface back to the current
                  project assembly (or something).

                  As long as it works, I'd stop digging
                  [else it might stop working again!]

                  Marc

                  Comment

                  • Michael Sander

                    #10
                    Re: System.Reflecti on.AssemblyBuil der: adding referenced assemblys

                    well, its only working in this tiny example. not in my app wich is far mor
                    complicated. i guess i will have to narrow this down.
                    thanks for your time and help!


                    "Marc Gravell" <marc.gravell@g mail.comschrieb im Newsbeitrag
                    news:uK6Dui$EJH A.3488@TK2MSFTN GP02.phx.gbl...
                    >I tried your code; it looks fine in reflector, so I imagine that VS is just
                    >having a hard time resolving the interface back to the current project
                    >assembly (or something).
                    >
                    As long as it works, I'd stop digging
                    [else it might stop working again!]
                    >
                    Marc

                    Comment

                    Working...