Public class test
{
Public int [] getdata (INT [] pargs)
{
Return pargs;
}
Public decimal [] getdata1 (decimal [] pargs)
{
Return pargs;
}
}
The above method may cause a lot of trouble when calling createinstance, and invokemethod may not be successful.
Question 1: parameters passed in: because each parameter must be completely matched when calling a method, object [] cannot be used.
Question 2: How to unbind the returned result as an array
Solution:
1: By using the list <> feature, createinstance is used to create an instance of the specified type. In this way, the returned toarray must be an array of the specified type.
2: Use array to convert the returned result array by default.
Code
Using system;
Using system. Data;
Using system. configuration;
Using system. LINQ;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. htmlcontrols;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. xml. LINQ;
Using system. collections;
Using system. Collections. Generic;
Using system. reflection;
Namespace test_webapp
{
Public class superconvert
{
Private object m_instanceofobj;
Private methodinfo m_addmethod;
Private methodinfo m_toarraymethod;
Public superconvert (string ptypename)
{
// Create by list <>
String vtypestring = string. format ("system. collections. generic. list '1 [[{0}, mscorlib, version = 2.0.0.0, culture = neutral, publickeytoken = b77a5c561934e089] ", ptypename );
This. m_instanceofobj = activator. createinstance (type. GetType (vtypestring ));
This. m_addmethod = This. m_instanceofobj.gettype (). getmethod ("add ");
This. m_toarraymethod = This. m_instanceofobj.gettype (). getmethod ("toarray ");
}
Public void add (Object pvalue)
{
This. m_addmethod.invoke (this. m_instanceofobj, new object [] {pvalue });
}
Public object toarray ()
{
Return this. m_toarraymethod.invoke (this. m_instanceofobj, null );
}
}
}
2. methodinfo vmethodinfo = vobjinstance. GetType (). getmethod ("methodname ");
Object vrtn = vmethodinfo. Invoke (vobjinstance, new object [] {123,345 });
Array vrtnlist = (array) vrtn;