473,396 Members | 1,971 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,396 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 2232
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Lenard Lindstrom | last post by:
I was wondering if anyone has suggested having Python determine a method's kind from its first parameter. 'self' is a de facto reserved word; 'cls' is a good indicator of a class method ( __new__...
2
by: seia0106 | last post by:
Hello I am writing a function to read a binary file. Here is a part of code #include <fstream> .. .. BYTE *pData; long lDataLen; pms->GetPointer(&pData); lDataLen = pms->GetSize(); // Read...
5
by: adrian | last post by:
hi all this is my first post to this group, so pls bear with me while i try to make some sense. i am trying to create a sproc which uses dynamic sql to target a particuar table eg. '.' and...
18
by: Christopher W. Douglas | last post by:
I am writing a VB.NET application in Visual Studio 2003. I have written a method that handles several events, such as closing a form and changing the visible status of a form. I have some code...
13
by: herrcho | last post by:
int intcmp(const void *a, const void *b) { return (*(int*)a - *(int*)b); } in the above , if i put just 'void' instead of 'const void' as a parameter, what's the difference ?
4
by: NotYetaNurd | last post by:
Hi all, I read that delegates are type safe and Functional pointers are not!!... in this context what does type safety mean? can anyone of you throw some light on it regards, ...
3
by: Ethan Strauss | last post by:
Hi, In C#.net 1.1, is there a simple way to determine if an arbitrary object can be cast as a number? How about if it can be cast as a int? The only ways I can find are to either try it and see if...
3
by: =?Utf-8?B?R2lkaQ==?= | last post by:
Hi, Using ASP .Net, I'm trying to create a ImageButton when the user clicks on a button, this is my code: protected void logout_btn_Click(object sender, ImageClickEventArgs e) {
2
by: Netwatcher | last post by:
Hello, i am new to c++ windows and DX programming, i encountered a code in the book stated in the title, which doesn't work for a reason, here is the code // Beginning Game Programming // Chapter...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.