473,395 Members | 1,629 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,395 software developers and data experts.

Cast object to a type at runtime



protected bool CompareTheValues(string expectedValue, string foundValue,
string valueType)
{
// Determine what the WMI data type was.
string typeString = "System." + valueType;
System.Type type = System.Type.GetType(typeString);
// Create the two objects for the comparison.
ObjectHandle objExpected = Activator.CreateInstance(type);
ObjectHandle objFound = Activator.CreateInstance(type);

switch(type.ToString())
{
case "System.UInt64":
objExpected = Convert.ToUInt64(expectedValue);
objFound = Convert.ToUInt64(foundValue);
break;
case "System.UInt32":
objExpected = Convert.ToUInt32(expectedValue);
objFound = Convert.ToUInt32(foundValue);
break;
case "System.UInt16":
case "System.UInt8":
objExpected = Convert.ToInt16(expectedValue);
objFound = Convert.ToUInt16(foundValue);
break;
case "System.SUInt16":
case "System.SUInt8":
objExpected = Convert.ToInt16(expectedValue);
objFound = Convert.ToInt16(foundValue);
break;
case "System.SUInt64":
objExpected = Convert.ToInt64(expectedValue);
objFound = Convert.ToInt64(foundValue);
break;
case "System.SUInt32":
objExpected = Convert.ToInt32(expectedValue);
objFound = Convert.ToInt32(foundValue);
break;
case "System.DateTime":
objExpected = Convert.ToDateTime(expectedValue);
objFound = Convert.ToDateTime(foundValue);
break;
case "System.Real64":
case "System.Real32":
default:
objExpected = Convert.ToDouble(expectedValue);
objFound = Convert.ToDouble(foundValue);
break;
}

switch(this.numericCompare)
{
case NumericComparer.DoesNotEqual:
break;
case NumericComparer.GreaterThan:
break;
case NumericComparer.GreaterThanOrEqual:
break;
case NumericComparer.LessThan:
break;
case NumericComparer.LessThanOrEqual:
// return (objExpected <= objFound) ? true : false;
break;
case NumericComparer.Equal:
return (objExpected.Equals(objFound)) ? true : false;
default:
break;
}

return false;
}
--
Steve
Nov 17 '05 #1
0 3333

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

Similar topics

0
by: Pankaj Jain | last post by:
Hi All, I have a class A which is derived from ServicesComponent to participate in automatic transaction with falg Transaction.Required. Class A is exposed to client through remoting on Http...
5
by: Radde | last post by:
HI, Are ther any pitfalls for dynamic cast in type safe downcasting..
0
by: Fidias Gil de Montes | last post by:
In a Distributed Windows application, I receive the following message when the client calls the server: ************** Exception Text ************** System.InvalidCastException: Unable to cast...
4
by: hazz | last post by:
I have inherited code but don't know exactly what this one line of code is doing? What it is the purpose of (ClassFileName)ClassFileName......... > ClassFileName vs2 = (ClassFileName...
17
by: Hazz | last post by:
In this sample code of ownerdraw drawmode, why does the '(ComboBox) sender' line of code need to be there in this event handler? Isn't cboFont passed via the managed heap, not the stack, into this...
3
by: mra | last post by:
I want to cast an object that I have created from a typename to the corresponding type. Can anycone tell me how to do this? Example: //Here, Create the object of type "MyClass" object...
1
by: Reza Nabi | last post by:
Bakground: I have a webform (LoadCtl.aspx) which loads the user control to a placeholder dynamically based on the ctlName querystring passed in the URL. Webform (LoadCtl.aspx) also passes a...
9
by: Jim in Arizona | last post by:
I get this error: Unable to cast object of type 'System.Web.UI.HtmlControls.HtmlInputText' to type 'System.Web.UI.WebControls.TextBox'. Using this code: Dim test3 As TextBox test3 =...
1
by: =?Utf-8?B?U2NvdHQ=?= | last post by:
Hello, Using VS2008 in a C# web service application, a class has been created that inherits from the ConfigurationSelection. This class file has been placed in the App_Code folder. The...
20
by: raylopez99 | last post by:
Inspired by Chapter 8 of Albahari's excellent C#3.0 in a Nutshell (this book is amazing, you must get it if you have to buy but one C# book) as well as Appendix A of Jon Skeet's book, I am going...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.