Home > Net >  For help on reflection mechanism instantiation non-static function
For help on reflection mechanism instantiation non-static function

Time:10-18

Beginners c # statement oneself first, for some problems is not quite understand, before a DLL file downloaded from the Internet, now you need to call this class,

 
The namespace DotNet. Utilities,
{
Public class EcanConvertToCh
{
Public string convertCh (string Chstr)
{
The Regex reg=new Regex (" ^ [\ u4e00 - \ u9fa5] $");//verify input Chinese characters
Byte [] arr=new byte [2].
String pystr="";
Int asc=0, M1=0, M2=0;
Char [] mChar=Chstr. ToCharArray ();//to get Chinese characters corresponding character array
For (int j=0; J & lt; MChar. Length; J++)
{
//if the input of Chinese characters
If (reg. IsMatch (mChar [j]. The ToString ()))
{
Arr=System. Text. Encoding. The Default. GetBytes (mChar [j]. The ToString ());
M1=(short) (arr [0]).
M2=(short) (arr [1]).
Asc=* 256 M1 + M2-65536;
If (asc & gt; 0 & amp; & Asc & lt; 160)
{
Pystr +=mChar [j];
}
The else
{
The switch (asc)
{
Case - 9254:
Pystr +="Zhen"; break;
Case - 8985:
Pystr +="Qian". break;
Case - 5463:
Pystr +="Jia". break;
Case - 8274:
Pystr +="Ge". break;
Case - 5448:
Pystr +="Ga". break;
Case - 5447:
Pystr +="La". break;
Case - 4649:
Pystr +="Chen"; break;
Case - 5436:
Pystr +="Mao". break;
Case - 5213:
Pystr +="Mao". break;
Case - 3597:
Pystr +="Die"; break;
Case - 5659:
Pystr +="Tian"; break;
Default:
For (int I=(getValue. Length - 1); I & gt;=0; I -)
{
If (getValue [I] <=(asc)//whether Chinese characters pinyin area code within the specified range
{
Pystr +=getName [I];//if not beyond the scope of the access to the corresponding pinyin
break;
}
}
break;
}
}
}
The else//if not Chinese characters
{
Pystr +=mChar [j]. The ToString ();//if not Chinese returns
}
}
Return pystr;//returns the access to Chinese characters, pinyin
}
}
}



Including convertCh method is static, as a rule should be at the time of call needs to be instantiated, but tried it many times, I don't know how to instantiate: the following code is aimed at the operation of the static function, the request for the static words should be how to call, how to instantiate?
 namespace relective 
{
Public partial class Form1: Form
{
Public _click ()
{
InitializeComponent();
}

Private void button1_Click (object sender, EventArgs e)
{
String dllName="DotNet. Utilities";//specify the Dll file name
String fullName="DotNet. Utilities. EcanConvertToCh";

The Assembly Dll=Assembly. The Load (dllName);//using the reflection function to load the Dll file
Type Type=Dll. GetType (fullName);//load the Dll files in the file object type
Var DLL=the Activator. CreateInstance (type);//create a constructor

String pinyin=type. InvokeMember (" convertCh ", BindingFlags InvokeMethod | BindingFlags. Public, null, null, the new object [] {textBox1. Text. The Trim ()}). The ToString ();
MessageBox. Show (pinyin);
}
}

CodePudding user response:

 
String dllName="DotNet. Utilities";//specify the Dll file name
String fullName="DotNet. Utilities. EcanConvertToCh";

The Assembly Dll=Assembly. The Load (dllName);//using the reflection function to load the Dll file
Type Type=Dll. GetType (fullName);//load the Dll files in the file object type
Var DLL=the Activator. CreateInstance (type);//create a constructor
MethodInfo convertCh=the GetMethod (" convertCh ");
String pinyin=convertCh. Invoke (DLL, new object [] {textBox1. Text. The Trim ()}). The ToString ();

CodePudding user response:

You put him into a static method does not become directly, this method completely don't need to set to the static

If you still want to use the static method calls, you need to instantiate the method in the class instance
Then use reflection calls this method
The InvokeMember (" convertCh, "BindingFlags InvokeMethod | BindingFlags. Public, null, DLL, new object [] {textBox1. Text. The Trim ()}). The ToString ();

Pay attention to is to put the second null into a DLL, mean to call DLL object on its way

CodePudding user response:

refer to the second floor stherix response:
you put him into a static method does not become directly, this method completely don't need to set to the static

If you still want to use the static method calls, you need to instantiate the method in the class instance
Then use reflection calls this method
The InvokeMember (" convertCh, "BindingFlags InvokeMethod | BindingFlags. Public, null, DLL, new object [] {textBox1. Text. The Trim ()}). The ToString ();

Pay attention to is to put the second null into a DLL, mean to call DLL object on its method


Thank you, because it is a beginner, so for these methods are not very familiar with, so static and non-static tried to use, to deepen our impression, I according to your ways of operation, will directly null replaced with DLL, will quote error: MissingMethod_Name, the method of 1/f, there is no problem, can be normal operation,

CodePudding user response:

reference 1st floor jinkuang45 response:
 
String dllName="DotNet. Utilities";//specify the Dll file name
String fullName="DotNet. Utilities. EcanConvertToCh";

The Assembly Dll=Assembly. The Load (dllName);//using the reflection function to load the Dll file
Type Type=Dll. GetType (fullName);//load the Dll files in the file object type
Var DLL=the Activator. CreateInstance (type);//create a constructor
MethodInfo convertCh=the GetMethod (" convertCh ");
String pinyin=convertCh. Invoke (DLL, new object [] {textBox1. Text. The Trim ()}). The ToString ();



Thank you, according to your ways to achieve instantiate and call the corresponding method,

CodePudding user response:

reference 4 floor wf7941817 response:
thanks, according to your ways to instantiate and call the corresponding method,


You're welcome, don't know why you want to call it ~

CodePudding user response:

reference 5 floor jinkuang45 reply:
Quote: refer to 4th floor wf7941817 response:

nullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C #
  • Related