Hello

I was working about TypeForwarding.  (
http://community.bartdesmet.net/blogs/bart/archive/2006/09/30/4479.aspx
)
So to implement it , I must add a reference and an attribute to a
specific assembly. I am ok with adding another
assembly's reference. But I had problem when adding the attribute.

The attribute i try to add is
System.Runtime.CompilerServices.TypeForwardedToAttribute
And its constructor takes one paramater -> System.Type

When I do this manually ( adding in assemblyinfo.cs and recompiling) ,
C# compiler add the lines below to manifest of assembly :

         .class extern forwarder Bar
         {
           .assembly extern newlib
        }

But when I try with my code , the resulting line in manifest is that :

 .custom instance void
[mscorlib]System.Runtime.CompilerServices.TypeForwardedToAttribute::.ctor(class
[mscorlib]System.Type) = ( 01 00 FF 00 00 )

As you my code results a difference but not the expected result . So
finally here the code I tried :

 public bool  AddTypeForwardingAttribute(AssemblyDefinition
assemblySource, AssemblyDefinition assemblyDestination,string
sTypeToForward)
        {
            // find System.Type of typeToForward

            System.Type typeToForward = null;
            System.Reflection.Assembly assembly =
System.Reflection.Assembly.LoadFrom(assemblyDestination.MainModule.Name);
            Type[] types = assembly.GetTypes();
            for (int i = 0; i < types.Length; i++)
            {
                Type currentType = types[i];
                if (currentType.Name == sTypeToForward)
                {

                    typeToForward = currentType ;
                    break;
                }
            }

            if (typeToForward == null ) return false;


             // add reference
            assemblySource.MainModule.Import(typeToForward );


             // add attribute
            CustomAttribute forwardAttribute = new
CustomAttribute(assemblySource.MainModule.Import
 
(typeof(System.Runtime.CompilerServices.TypeForwardedToAttribute).GetConstructor
                                           (new Type[]
{ typeof(System.Type) })));

 
forwardAttribute.ConstructorParameters.Add((object)typeToForward);


            assemblySource.CustomAttributes.Add(forwardAttribute);

            // everything is ok
            return true;
        }

Best Regards,
Akin Ocal
--~--~---------~--~----~------------~-------~--~----~
--
mono-cecil
-~----------~----~----~----~------~----~------~--~---

Reply via email to