471,605 Members | 1,728 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Dynamiclly Determine Type of Parameter and Type Cast it (code insi

Hello,

Here's a little background. I have a function that calls a method of another
assembly (COM Interop) dynamically through Reflection. It gets method name
and array of values and calls .InvokeMember to get results. Everything worked
fine until parameters types of Boolean and DateTime appeared, it caused Type
mismatch exception. So I decided to explicitlly convert to Bool and DateTime,
however it would not work, only Bool.Parse() and DateTime.Parse seems to work.
But I still want to have a better way of handling it...
Considering my code snippet bellow, could you help me with the following
questions:
1. Is there a way to dynamically get "String representation" of a type that
could be used in a Switch case statement.
2. Why type.Fullname returns "System.Boolean&" what's & charecter doing
there at the end of string
3. Is the code below the best way to handle this task?
/**********Code snippet*****/
Assembly asm = Assembly.Load("Interop.Project1");
//create new instance of class
Type type = asm.GetType("Project1.Class1Class");
object t1 = Activator.CreateInstance(type);
object result;

ParameterModifier[] paramMod = new ParameterModifier[1];
ParameterModifier mod = new ParameterModifier(3);
mod[0] = false;
mod[1] = true;
mod[2] = true;
paramMod[0] = mod;

object[] argList = new object[3];
argList[0] = "some string";
argList[1] = "False";
argList[2] = "5/13/2006";

//get Method Info and Parameter info
MethodInfo mInfo = type.GetMethod("doA");
ParameterInfo[] paramsInfo = mInfo.GetParameters();

//Type paramType = paramsInfo[2].ParameterType;

for (int i=0;i<paramsInfo.Length;i++)
{

switch (paramsInfo[i].ParameterType.FullName )
{
case "System.Boolean&" :
argList[i] = Boolean.Parse(argList[i].ToString());
break;
case "System.DateTime&" :
argList[i] = DateTime.Parse(argList[i].ToString());
break;
default:
argList[i] = System.Convert.ChangeType(argList[i],
paramsInfo[i].ParameterType);
break;
}

}
//invoke the method name passed m
result = type.InvokeMember("doA",
BindingFlags.InvokeMethod,
null,
t1,
argList, paramMod, null, null);

Dec 14 '05 #1
1 2170
Lenn,

I would check against the type directly, like this:

if (paramsInfo[i].ParameterType == typeof(bool))
{
// Perform your logic here.
}

However, I probably would just make a call to ConvertType every time for
this, since you just want to convert between simple types it seems.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Lenn" <Le**@discussions.microsoft.com> wrote in message
news:9B**********************************@microsof t.com...
Hello,

Here's a little background. I have a function that calls a method of
another
assembly (COM Interop) dynamically through Reflection. It gets method name
and array of values and calls .InvokeMember to get results. Everything
worked
fine until parameters types of Boolean and DateTime appeared, it caused
Type
mismatch exception. So I decided to explicitlly convert to Bool and
DateTime,
however it would not work, only Bool.Parse() and DateTime.Parse seems to
work.
But I still want to have a better way of handling it...
Considering my code snippet bellow, could you help me with the following
questions:
1. Is there a way to dynamically get "String representation" of a type
that
could be used in a Switch case statement.
2. Why type.Fullname returns "System.Boolean&" what's & charecter doing
there at the end of string
3. Is the code below the best way to handle this task?
/**********Code snippet*****/
Assembly asm = Assembly.Load("Interop.Project1");
//create new instance of class
Type type = asm.GetType("Project1.Class1Class");
object t1 = Activator.CreateInstance(type);
object result;

ParameterModifier[] paramMod = new ParameterModifier[1];
ParameterModifier mod = new ParameterModifier(3);
mod[0] = false;
mod[1] = true;
mod[2] = true;
paramMod[0] = mod;

object[] argList = new object[3];
argList[0] = "some string";
argList[1] = "False";
argList[2] = "5/13/2006";

//get Method Info and Parameter info
MethodInfo mInfo = type.GetMethod("doA");
ParameterInfo[] paramsInfo = mInfo.GetParameters();

//Type paramType = paramsInfo[2].ParameterType;

for (int i=0;i<paramsInfo.Length;i++)
{

switch (paramsInfo[i].ParameterType.FullName )
{
case "System.Boolean&" :
argList[i] = Boolean.Parse(argList[i].ToString());
break;
case "System.DateTime&" :
argList[i] = DateTime.Parse(argList[i].ToString());
break;
default:
argList[i] = System.Convert.ChangeType(argList[i],
paramsInfo[i].ParameterType);
break;
}

}
//invoke the method name passed m
result = type.InvokeMember("doA",
BindingFlags.InvokeMethod,
null,
t1,
argList, paramMod, null, null);

Dec 14 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Lenard Lindstrom | last post: by
2 posts views Thread by seia0106 | last post: by
18 posts views Thread by Christopher W. Douglas | last post: by
13 posts views Thread by herrcho | last post: by
4 posts views Thread by NotYetaNurd | last post: by
3 posts views Thread by Ethan Strauss | last post: by
3 posts views Thread by =?Utf-8?B?R2lkaQ==?= | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by CCCYYYY | last post: by

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.