Metadata of Assembly in C#

In this article we will discuss about how to get the metdata of an Assembly in C#.
  • 3356

For this purpose we will use the reflection concept. Reflection is the concept which is used to get metadata (information or members) of an assembly. To implement the reflection we use some classes reside within System.Reflection namespace. It is used toThe System.Reflection namespace defines the following types to analyze the module's metadata of an assembly: Assembly, Module, Enum, ParameterInfo, MemberInfo, Type, MethodInfo, ConstructorInfo, FieldInfoEventInfo, and PropertyInfo.

Get metadata of the class present in the assembly that we have created and also all of its information. Here is the code:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Reflection;

 

namespace reflection

{

    class Program

    {

        static void Main(string[] args)

        {

            Assembly asm = Assembly.LoadFrom(@"D:\meghawebsite\reflassm\reflassm\bin\Debug\reflassm.dll");

            Type[] type = asm.GetTypes();

            foreach (Type t in type)

            {

                Console.WriteLine(t.Name);

                Console.WriteLine(t.MemberType.ToString()+"\n");

                if (t.Name == "mycls")

                {

                    MemberInfo[] minfo = t.GetMembers();

                    foreach(MemberInfo m in minfo)

                    {

                        Console.WriteLine("\n"+m.Name);

                        Console.WriteLine(m.MemberType.ToString());

                    }

                }

            }

            Console.ReadLine();

        }

    }

}

 

Output:

  image2.jpg

In the above output it it clear that we have create the mycls class in the assembly fun and methods name and age field etc.

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

© 2018 DotNetHeaven. All rights reserved.