Talk about it today. NET, how do we know which corner of this type is on the hard disk when we get a type through reflection? For example, if we need to ask the server to dynamically load a data source, how does the server know where the data source is?
Most of the tutorials on the Web are written, you can use the Assembly.Load method to load the assembly first, and then use the Assembly.GetType or Assembly.gettypes method to process it.
This method is very good and practical, basically enough. But if it is so boring, even if not cold knowledge, it is not necessary to write these.
If there is a way to automatically search the assembly if there is no exposure to the corresponding type, why should we also load the assembly itself? Are the small and soft groups so boring? There really is a way to solve this problem.
Type.GetType, it's you.
So what's magical about this approach? Type.GetType has multiple overloads, in addition to one without parameters, the remaining overloads require at least one string type of typename to be searched, see MSDN. For example:
Using System;
Namespace ConsoleApplication2
{
Internal class Program
{
private static void Main (string[] args)
{
Type Addedinruntimetype = Type.GetType ("Liba.testclass, Liba, version=1.0.0.0, Culture=neutral, PublicKeyToken=null") ;
foreach (Var propertyInfo in addedinruntimetype.getproperties ())
{
Console.WriteLine (Propertyinfo.name);
}
}
}
}
Suppose we have a liba.dll,liba.dll inside the directory contains a class Liba.testclass, the above code can get all the property names inside, and then to do something about this type of shame ashamed of the things that you crossing decided on their own.
However, there is still one limitation that is not resolved. The parameters of the GetType method are only typename with respect to the file. But the goods did not specify the path. If the assembly to be loaded is in the GAC or under the current assembly path, what if the Xian (DE) Origin (Dan) (Teng) needs to be placed in a subdirectory? For example, in the sub-directory "Runtime" and "Runtme2" under the search for what to do? Or put the code directlyTOEFL Answerswww.jamo123.com
Using System;
Namespace ConsoleApplication2
{
Internal class Program
{
private static void Main (string[] args)
{
AppDomain.CurrentDomain.AppendPrivatePath ("Runtime");
AppDomain.CurrentDomain.AppendPrivatePath ("runtime2");
Type Addedinruntimetype = Type.GetType ("Liba.testclass, Liba, version=1.0.0.0, Culture=neutral, PublicKeyToken=null") ;
foreach (Var propertyInfo in addedinruntimetype.getproperties ())
{
Console.WriteLine (Propertyinfo.name);
}
}
}
}
AppDomain.CurrentDomain.AppendPrivatePath can increase the path of the CLR search, but this method has been marked as obsolete. Please disregard this warning yourself. Or, follow the code below:
#pragma warning Disable 618
AppDomain.CurrentDomain.AppendPrivatePath ("Runtime");
#pragma warning Restore 618
Continue to say, in fact, this path can also be written in the configuration file. MSDN instructions here, examples are as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<configuration>
<runtime>
<assemblybinding xmlns= "Urn:schemas-microsoft-com:asm.v1" >
<probing privatepath= "Runtime;runtime2"/>
</assemblyBinding>
</runtime>
</configuration>
. Net AppDomain.CurrentDomain.AppendPrivatePath (@ "Libs");