The System.Type class defines a number of members that can be used to examine a type’s metadata, a great number of which return types from the System.Reflection namespace.
The act of loading external assemblies on demand is known as a dynamic load.
System.Reflection defines a class Assembly. Which enables to dynamically load an assembly and discover properties about the assembly
Assembly type enables to dynamically load private or shared assemblies, as well as load an assembly located at an arbitrary location.
1: 2: 3: 4: |
public class Assembly {
public static Assembly Load(AssemblyName assemblyRef);
public static Assembly Load(String assemblyString);
}
|
The CLR forbids any code in the assembly from executing while using ReflectionOnlyLoadFrom or ReflectionOnlyLoad. (Suitable for types investigation).
1: 2: 3: 4: |
public class Assembly {
public static Assembly ReflectionOnlyLoadFrom(String assemblyFile);
public static Assembly ReflectionOnlyLoad(String assemblyString);
}
|
If you’re writing an application that will dynamically discover and construct type instances, you should take one of the following approaches:
Have the types derive from a base type that is known at compile time. At run time, construct an instance of the derived type, place the reference in a variable that is of the base type (by way of a cast), and call virtual methods defined by the base type.
Have the type implement an interface that is known at compile time. At run time, construct an instance of the type, place the reference in a variable that is of the interface type (by way of a cast), and call the methods defined by the interface.
Late binding is a technique in which you are able to create an instance of a given type and invoke its members at runtime without having hard-coded compile-time knowledge of its existence.
The System.Activator class (defined in mscorlib.dll) is the key to the .NET late-binding process.
In a nutshell, attributes are nothing more than code annotations that can be applied to a given type (class, interface, structure, etc.), member (property, method, etc.), assembly, or module.
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: |
// This class can be saved to disk.
[Serializable]
public class Motorcycle
{
// However, this field will not be persisted.
[NonSerialized]
float weightOfCurrentPassengers;
// These fields are still serializable.
bool hasRadioSystem;
bool hasHeadSet;
bool hasSissyBar;
}
|
Attributes are shipped with types metadata