Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / December 2005

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Lenn - 14 Dec 2005 20:25 GMT
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);
Nicholas Paldino [.NET/C# MVP] - 14 Dec 2005 21:24 GMT
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.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Hello,
>
[quoted text clipped - 70 lines]
> t1,
> argList, paramMod, null, null);

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.