.NET metadata is set of tables that mark all type definitions and referenced types.
TypeDef #n ( Type Definition ) represent a type defined within the current assembly.
TypeRef #n ( Type Reference ) is a pointer to the referenced type's full metadata definition in an external assembly.
TypeDefName contains the name of given type and Extends token contains the base type. String User token is represent all the string literal in your code.
Reflection:
Process of runtime type discovery. Reflection let you access all metadata information in your code (System.Reflection namespace). the System.Type class got some members that can be used to examine type metadata.( e.g. Type.GetMethods and Type.GetFields )
Type is an abstract class. System.Object defines a method named GetType(), which returns an instance of the Type class that represents the metadata for current object.
AnyType my_any_type = new AnyType();
Type my_type =
my_any_type.GetType();
OrC# typeof() operation, which doesn't need object instance:
Type my_type = typeof(AnyType);
OrUsing System.Type.GetType() static method and specify the fully qualified string name of the type. unlike the above approaches you do not need to have a compiled time knowledge of the type. This method got two overloads (two boolean parameters one for throw an exception if the type is not found and another one for case sensitivity)
Dynamic Load:
Loading external assemblies on demand. Assembly class in System.Reflection gives you load dynamically ability by help of methods such as Load() and LoadFrom(). LoadFrom() method is more flexible you can enter an absolute path. You are also able to supply <codeBase> value.
Late Binding:
Create an instance of a type and invoke its member at runtime without knowing its existence at compile time. Therefore manifest have no direct listing of the assembly. For .NET late binding process we use System.Activator class. Activator.CreateInstance() method will help you to allocate an entity into memory on the fly which got several overload. Above method return System.Object and you are not able using explicit cast to cast to strong type since your program has not set a reference to corresponding assembly. here is an example:
// Try to load a local copy of any
assembly.
Assembly asm = null;
try
{
asm = Assembly.Load("AnyAssemblyName");
}
catch (FileNotFoundException ex)
{
Console.WriteLine(ex.Message);
}
if (asm != null)
{
//
Get metadata for any type.
Type any_type = asm.GetType("AnyAssemblyName.AnyTypeName");
//
Create an instance form any_type on the fly.
object obj = Activator.CreateInstance(any_type);
Console.WriteLine("Created a {0}
using late binding!", obj);
//
Get info for any method.
MethodInfo mi = any_type.GetMethod("AnyMethodName");
//
Invoke method ('null' for no parameters).
mi.Invoke(obj, null);
}
In case your method got any parameter, you need to package up the arguments as loosely typed array of objects:
// Invoke any mehtod with arguments
(first bool and second enum).
MethodInfo mi = any_type.GetMethod("AnyMethodName");
.NET Attributes:
Code annotations that can be applied to type, member, assembly or module which embed additional metadata into an assembly. They are class type that extend the abstract System.Attribute base class. An Attribute applies to the very next item. To apply multiple attributes to a single item use comma-delimited list or alternatively put them as single attribute after each other:
[Serializable,
Obsolete("This class is
Obsoleted!")]
Named Property Syntax: when attribute got .NET writable property you may set the property by this syntax.
[AnyAttributeClass(AnyPropertyName
= "property_value")]
Custom attributes can be applied to any aspect of your code (methods, classes, properties and so forth). You can restrict the attribute that can only applied to select code elements through [AttributeUsage] attribute which allows any combination of values from AttributeTargets enumeration. [AttributeUsage] attribute has AllowMultiple named property which specify whether attribute can be applied more than once on the same item ( default is false ), and Inherited named property shows whether the attribute should be inherited by derived classes ( default is true ). Mentioned named property and [AttributeUsage] should be on the definition of the custom attribute. like so
[AttributeUsage(AttributeTargets.Class
| AttributeTargets.Struct,
AllowMultiple = false,
Inherited = false)]
public sealed class AnyAttributeClass
: System.Attribute
You may also apply attributes on assembly level using [assembly:] tag. It is best practice that you place the assembly level attributes at top of the C# source code after using statements and before namespaces.
[assembly: CLSCompliant(true)]
Visual Studio projects got a AssemblyInfo.cs file, which is good place for assembly level attributes.Expendable application should have following characteristics:
- Must provide some input vehicle to allow the user to specify the module to plug in. (require dynamic loading)
- Able to determine whether the module support the correct functionality in order to be plugged in to the environment (requires reflection)
- Obtain a reference to required infrastructure and invoke the member to trigger the underlying functionality. (May requires late binding)
References:
Pro C# 5.0 and the .NET 4.5 Platform by Andrew Troelsen