locked
copy meta data from DLL to DLL RRS feed

  • Question

  • Can i copy the meta data of one dll into another dll using reflection? Is it possible?

     


    maifs
    • Moved by Leo Liu - MSFT Wednesday, May 25, 2011 9:16 AM Moved for better support. (From:Visual C# General)
    Monday, May 23, 2011 4:56 AM

Answers

  •  

    Hi Mehmood,

     

    Welcome to the MSDN forum.

     

    I have written an example for you to obtain metadata in an assembly below:

    public static void ShowAssemblies()

            {

                Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();

                Console.WriteLine("\nAssemblies loaded in current AppDomain:");

                int i = 1;

                foreach (Assembly aa in assemblies)

                {

                    Console.WriteLine("{0}. {1}", i++, aa.FullName);

                }

            }

     

            static void Main(string[] args)

            {

                string assemblyFileName = "";

                Console.WriteLine("Please input assembly name:");

                assemblyFileName=Console.ReadLine();

     

                try

                {

                    ShowAssemblies();

                    Assembly a = Assembly.LoadFrom(assemblyFileName);

                    Console.WriteLine("\nLoad Assembly: {0}...", a.FullName);

                    ShowAssemblies();

     

                    Console.WriteLine("Name:            " + a.FullName);

                    Console.WriteLine("Version:          " + a.GetName().Version);

                    Console.WriteLine("CodeBase:        " + a.CodeBase);

                    Console.WriteLine("Location:            " + a.Location);

                    Console.WriteLine("EntryPoint:          " + a.EntryPoint);

                    Console.WriteLine("Is in GAC:" + a.GlobalAssemblyCache);

                    Console.WriteLine("CLR Version:    " + a.ImageRuntimeVersion);

     

                    // obtain Types in assembly

                    Type[] types = a.GetTypes();

                    Console.WriteLine("\nTypes in Assembly:");

                    foreach (Type type in types)

                    {

                        Console.WriteLine("Type:          " + type.Name);

                    }

     

                    // referenced assemblies

                    AssemblyName[] appAssemblies = a.GetReferencedAssemblies();

                    Console.WriteLine("\nReferenced Assemblies:");

                    foreach (AssemblyName assembly in appAssemblies)

                    {

                        Console.WriteLine(assembly);

                    }

                }

                catch

                {

                    Console.WriteLine("{0} is not a correct assembly name", assemblyFileName);

                }

            }

     

    I hope this can help you.

    Have a nice day!


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Paul Zhou Monday, May 30, 2011 8:14 AM
    Thursday, May 26, 2011 7:42 AM

All replies

  • I am moving your thread into the Common Language Runtime Forum for specialized support. Thanks.
    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, May 25, 2011 9:17 AM
  •  

    Hi Mehmood,

     

    Welcome to the MSDN forum.

     

    I have written an example for you to obtain metadata in an assembly below:

    public static void ShowAssemblies()

            {

                Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();

                Console.WriteLine("\nAssemblies loaded in current AppDomain:");

                int i = 1;

                foreach (Assembly aa in assemblies)

                {

                    Console.WriteLine("{0}. {1}", i++, aa.FullName);

                }

            }

     

            static void Main(string[] args)

            {

                string assemblyFileName = "";

                Console.WriteLine("Please input assembly name:");

                assemblyFileName=Console.ReadLine();

     

                try

                {

                    ShowAssemblies();

                    Assembly a = Assembly.LoadFrom(assemblyFileName);

                    Console.WriteLine("\nLoad Assembly: {0}...", a.FullName);

                    ShowAssemblies();

     

                    Console.WriteLine("Name:            " + a.FullName);

                    Console.WriteLine("Version:          " + a.GetName().Version);

                    Console.WriteLine("CodeBase:        " + a.CodeBase);

                    Console.WriteLine("Location:            " + a.Location);

                    Console.WriteLine("EntryPoint:          " + a.EntryPoint);

                    Console.WriteLine("Is in GAC:" + a.GlobalAssemblyCache);

                    Console.WriteLine("CLR Version:    " + a.ImageRuntimeVersion);

     

                    // obtain Types in assembly

                    Type[] types = a.GetTypes();

                    Console.WriteLine("\nTypes in Assembly:");

                    foreach (Type type in types)

                    {

                        Console.WriteLine("Type:          " + type.Name);

                    }

     

                    // referenced assemblies

                    AssemblyName[] appAssemblies = a.GetReferencedAssemblies();

                    Console.WriteLine("\nReferenced Assemblies:");

                    foreach (AssemblyName assembly in appAssemblies)

                    {

                        Console.WriteLine(assembly);

                    }

                }

                catch

                {

                    Console.WriteLine("{0} is not a correct assembly name", assemblyFileName);

                }

            }

     

    I hope this can help you.

    Have a nice day!


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Paul Zhou Monday, May 30, 2011 8:14 AM
    Thursday, May 26, 2011 7:42 AM