473,326 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Reflection Array Parameter Error

Hi All,
I am facing a problem while using Reflection.
I had written a simple class containing simple method taking two
parameters

1. Int Arrary
2. bool

This can be achieved by following code.

using System;
using System.Reflection;
namespace TestProject
{
public class TestClass
{
public void Method1(int[] IDArray, bool SomethingElse)
{
foreach (int i in IDArray)
{
Console.WriteLine (i);
}
}
static void Main()
{
object instance =
Activator.CreateInstance(typeof(TestClass));
MethodInfo method =
typeof(Class1).GetMethod("Method1");
int[] IDArray = new int[3]{1,2,3};
object[] Parameters = new object[2];
Parameters[0] = IDArray;
Parameters[1] = true;
method.Invoke(instance, Parameters);
}
}
}
As you can see I created a int array. But there are some scenario where
this "int" type is not known to me until runtime.
That's why I replaced the line
"int[] IDArray = new int[3]{1,2,3};"
With
"object[] IDArray = new object[3]{1,2,3};
When I tried to execute this I am getting error
"An unhandled exception of type 'System.ArgumentException' occurred
in mscorlib.dll
Additional information: Object type cannot be converted to target
type."

Am I doing something wrong here, your help in this regard will be
appreciated.

Nilesh

Sep 7 '06 #1
1 1663

I suggest you try Array.CreateInstance.

ni************@gmail.com wrote:
Hi All,
I am facing a problem while using Reflection.
I had written a simple class containing simple method taking two
parameters

1. Int Arrary
2. bool

This can be achieved by following code.

using System;
using System.Reflection;
namespace TestProject
{
public class TestClass
{
public void Method1(int[] IDArray, bool SomethingElse)
{
foreach (int i in IDArray)
{
Console.WriteLine (i);
}
}
static void Main()
{
object instance =
Activator.CreateInstance(typeof(TestClass));
MethodInfo method =
typeof(Class1).GetMethod("Method1");
int[] IDArray = new int[3]{1,2,3};
object[] Parameters = new object[2];
Parameters[0] = IDArray;
Parameters[1] = true;
method.Invoke(instance, Parameters);
}
}
}
As you can see I created a int array. But there are some scenario where
this "int" type is not known to me until runtime.
That's why I replaced the line
"int[] IDArray = new int[3]{1,2,3};"
With
"object[] IDArray = new object[3]{1,2,3};
When I tried to execute this I am getting error
"An unhandled exception of type 'System.ArgumentException' occurred
in mscorlib.dll
Additional information: Object type cannot be converted to target
type."

Am I doing something wrong here, your help in this regard will be
appreciated.

Nilesh
Sep 7 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
4
by: Tim Werth | last post by:
I am trying to use reflection to add an event handler for the RowUpdated event of the OracleDataAdapter object (ODP.NET), but the same thing can be said for SqlDataAdapter if you only use...
6
by: Lars Nielsen | last post by:
Hi everybody, I've run into a problem when using the reflection assembly on COM objects and been browsing around the net for some while. Apparently a few has asked the same question though no...
1
by: Mike Malter | last post by:
I am just starting to work with reflection and I want to create a log that saves relevant information if a method call fails so I can call that method again later using reflection. I am...
3
by: Venkat | last post by:
This is the piece of code in Early Binding and works as expected. --------------------------------------------------------------------------- using ControlLicenseService LicenseInfoClient lic =new...
3
by: trevorelbourne | last post by:
Hi, I am having trouble accessing the elements of an array using reflection. This is the code I am having trouble with: FieldInfo Fields = Obj.GetType().GetFields(); foreach (FieldInfo fi in...
12
by: Gus | last post by:
Hi, I´m triying to invoke a member using reflection, but this member requiere two parameters, the first patrameter is a string and the second is a reference to a dataset (output parameter). The...
1
by: Rathish P S | last post by:
Hi friends, I am trying with Reflection and Generics in C#. But I get confused with some of these new features. I am trying to get the type of generic parameter with the...
4
by: puzzlecracker | last post by:
I am going to post the most relavent code that causes this behavior: Here I try to invoke a static method of TestProgram Class...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.