Createinstance-array Transfer

Source: Internet
Author: User

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;

 

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.