Use activator. creatinstance to dynamically create the same Assembly object

Source: Internet
Author: User
In the normal factory class, we will have a lot of switch cases and so on. How to avoid them, we can use. Net activator to help us do this simply. Currently, after a simple big test, public constructor is supported, and internal private has not been carefully checked because I am concerned about public constructor.
The reason for this research is that I want to dynamically create a dataaccess object in the form of a configuration file.
The main reason is that my constructor parameter is a custom class, not the basic data type of the system. I did not know what went wrong during the test yesterday. So I wrote the following test this morning: Code I found that I really like snippet compiler. Using System;
Using System. reflection;
Using System. collections;
Namespace Fishsky. Testing
{
Public   Class Desiresay
{
Public Desiresay ()
{
}
Private   String Mwords;
Public   String Words
{
Get {ReturnMwords ;}
Set {Mwords=Value ;}
}
}
Public   Class Sayhello
{
Private   String Miwannasay = "" ;
Private Desiresay mdesiresay;
Private   Int Minternalint =- 1 ;
Public Sayhello ()
{
}
Public Sayhello ( String Iwannasay)
{
Miwannasay=Iwannasay;
}
Public Sayhello (desiresay)
{
Mdesiresay=Desiresay;
}
Internal Sayhello ( Int Internalint)
{
Minternalint=Internalint;
}
Public   String Hello ()
{
If (Minternalint ! =- 1 )
{
ReturnMinternalint. tostring ();
}
Else
{
If (Mdesiresay ! = Null )
{
ReturnMdesiresay. words;
}
Else
{
If (Miwannasay = "" )
Return   " Hello " ;
Else
Return Miwannasay;
}
}
}

}
Public   Class Myclass
{
Public   Static   Void Main ()
{
String Typename = " Fishsky. Testing. sayhello " ;
Type type = Type. GetType (typename, True );
Sayhello hello = (Sayhello) activator. createinstance (type );
WL ( " Sayhello says {0} " , Hello. Hello ());
Console. writeline ( " Next test with string parameter in constructor .. " );
Sayhello hello2 = (Sayhello) activator. createinstance (type, New Object [] {"I wanna say hello!"} );
WL ( " Sayhello says {0} " , Hello2.hello ());
Console. writeline ( " Next test with class parameter in constructor .. " );
Desiresay = New Desiresay ();
Desiresay. Words = " I desire say! Let me say! " ;
Sayhello hello3 = (Sayhello) activator. createinstance (type, New Object [] {Desiresay} );
WL ( " Sayhello says {0} " , Hello3.hello ());
// Console. writeline ("next test with int parameter in internal constructor ..");
// Sayhello hello4 = (sayhello) activator. createinstance (type, new object [] {250 });
// // Sayhello hello4 = (sayhello) activator. createinstance (type, bindingflags. nonpublic, null, new object [250], new cultureinfo ());
// WL ("sayhello says {0}", hello4.hello ());
RL ();
}

Helper Methods # Region Helper Methods

Private   Static   Void WL ( Object Text, Params   Object [] ARGs)
{
Console. writeline (text. tostring (), argS );
}

Private   Static   Void RL ()
{
Console. Readline ();
}

Private   Static   Void Break ()
{
System. Diagnostics. Debugger. Break ();
}

# Endregion
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.