Click here to Skip to main content
15,886,813 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a scenario in which i want to to convert a value in the dataType known at runtime. I have tried hard to solve it but could not find a solution, have a look at the code and guide me... thanx
C#
private void FormatToDbType(SqlDbType dbTp, string value)
        {
            switch (dbTp)
            {
                //BigInt64
                case SqlDbType.BigInt:
                    val = Convert.ToInt64(value);
                    break;
                //Binary
                case SqlDbType.Binary:
                    val = Convert.ToInt64(value);(whatever code)
                    break;
                //Bit
                case SqlDbType.Bit:
                (whatever code);
                break;
                //Char
              }
        }

now i want to pass a value in string and get it out in desired Converted DataType.
what type of variable should i use.
i have tried to use generic aswell
but it gives error
Posted
Updated 29-Oct-13 23:16pm
v2
Comments
BillWoodruff 30-Oct-13 5:54am    
Where are you defining the variable 'val and how are you using it ?
raeeschaudhary 30-Oct-13 8:15am    
var is not feasible here, i guess
Bheeshm 30-Oct-13 8:24am    
In the Method , Change the DataType of "value" variable to dynamic
and you can Get the Type by .... value.GetType().FullName
Bheeshm 30-Oct-13 8:26am    
static void Main(string[] args)
{
MyMethod(1);
}

static void MyMethod(dynamic xx )
{
xx.GetType().FullName; //This will give you the Name of the DataType
}

1 solution

You can do it using reflection. Here is the idea: you get the type and inquire for its constructors. You need to know the purpose of each constructor in advance, so most usually this is done based on parameterless constructor, or some constructor signature you specifically design for this purpose. (Apparently, you want to do it for some set of types. If it was just one type, you would simply hard-code the construction of the object. So, you can create some constructor signature shared by all the types you want to instantiate programmatically and make sure all your types implement it.)

If you simply can use default constructor, you can call System.Activator.CreateInstance:
http://msdn.microsoft.com/en-us/library/wccyzw83.aspx[^].

In more general case, you use one of the reflection methods named "GetConstructor" or "GetConstructors" to get and instance of the class System.Reflection.ConstructorInfo or array of such instances:
http://msdn.microsoft.com/en-us/library/system.type.aspx[^],
http://msdn.microsoft.com/en-us/library/h93ya84h.aspx[^].

If this is the array of System.Reflection.ConstructorInfo instances, you traverse this array to pick one you need, typically by finding out the one with the signature you need, which you can do looking at the list of parameters:
http://msdn.microsoft.com/en-us/library/system.reflection.methodbase.getparameters.aspx[^].

When you found appropriate constructor, you use on of the invoke methods to create an object:
http://msdn.microsoft.com/en-us/library/6ycw1y17.aspx[^],
http://msdn.microsoft.com/en-us/library/a89hcwhh.aspx[^],
http://msdn.microsoft.com/en-us/library/x7xy3xtx.aspx[^],
http://msdn.microsoft.com/en-us/library/4k9x6bc0.aspx[^].

And finally, to use the object as the object of required type, you will need to cast it to required type.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900