// Loading the current assembly Assembly myAsm = Assembly.GetExecutingAssembly(); // Showing some properties MessageBox.Show(myAsm.FullName); MessageBox.Show(myAsm.Location); // Getting some assembly attribute Type attrType = typeof(AssemblyDescriptionAttribute); object[] attrs = myAsm.GetCustomAttributes(attrType, false); foreach (Attribute attr in attrs) { AssemblyDescriptionAttribute desc = attr as AssemblyDescriptionAttribute; MessageBox.Show(desc.Description); } // Getting types in this assembly Type[] types = myAsm.GetTypes(); foreach (Type t in types) MessageBox.Show(t.ToString()); // Getting more information about an object MemberInfo[] minfos = typeof(Person).GetMembers(); foreach (MemberInfo mi in minfos) MessageBox.Show(mi.ToString()); // Getting attributes for an object Type tstring = typeof(string); foreach (Attribute attr in tstring.GetCustomAttributes(false)) MessageBox.Show(attr.ToString()); //.GetType().Name);Assembly static methods:
GetAssembly
GetCallingAssembly
GetEntryAssembly
GetExecutingAssembly
Load
LoadFile
LoadFrom
ReflectionOnlyLoad
ReflectionOnlyLoadFrom
Some useful methods: Location
FullName
GetCustomAttributes
GetExportedTypes
GetModules/GetModule
GetTypes
Type class properties:
Assembly
Attributes
IsClass
IsEnum
IsInterface
IsPrimitive
... and others
Type class methods:
GetConstructor/GetConstructors
GetEvent/GetEvents
GetField/GetFields
GetMember/GetMembers
GetMethod/GetMethods
GetProperty/GetProperties
... and others
MemberInfo derived classes:
ConstructorInfo
EventInfo
FieldInfo
LocalVariableInfo
MethodBase
MethodInfo
PropertyInfo
BindingFlags enumeration:
DeclaredOnly - Members directly declared on the specific type are included. Inherited members are ignored.
FlattenHierarchy - Declared and inherited members should be returned, but also returns protected members.
IgnoreCase - A case-insensitive matching of the member name should be used.
Instance - Members that are part of an instance of type will be included.
NonPublic - Members that are not public are included.
Public - Members that are public are included.
Static - Members that are defined as static are included.
Sample:
Type tperson = typeof(Person); BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; MemberInfo[] members = tperson.GetMembers(flags); foreach (MemberInfo member in members) MessageBox.Show(member.Name);More information: Attributes (An example)
A simple activities manager
0 comments:
Post a Comment