如何在运行时创建已知的数据类型实例。 [英] How to create a instance of data type known at runtime.

查看:77
本文介绍了如何在运行时创建已知的数据类型实例。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个场景,我想在运行时已知的dataType中转换一个值。我已经努力解决它但找不到解决方案,查看代码并指导我... thanx

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

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
              }
        }



现在我想传递一个字符串中的值并将其输出到所需的转换数据类型中。

我应该使用什么类型的变量。

i试图使用泛型

但它给出了错误


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

推荐答案

您可以使用反射来完成。这是一个想法:你得到类型并查询其构造函数。您需要事先了解每个构造函数的用途,因此通常这是基于无参数构造函数或您为此目的专门设计的一些构造函数签名。 (显然,你想要为一些类型的代码做。如果它只是一种类型,你只需要对对象的构造进行硬编码。因此,你可以创建一些由你想要实例化的所有类型共享的构造函数签名。以编程方式确保所有类型都实现它。)



如果您只是可以使用默认构造函数,则可以调用 System.Activator.CreateInstance

http://msdn.microsoft.com /en-us/library/wccyzw83.aspx [ ^ ]。



在更一般的情况下,您使用名为GetConstructor或GetConstructors的反射方法之一获取和实例类 System.Reflection.ConstructorInfo 或此类实例的数组:

http://msdn.microsoft.com/en -us / library / system.type.aspx [ ^ ],

http:// msdn .microsoft.com / zh-CN / library / h93ya84h.aspx [ ^ ]。



如果这是 System.Reflection.ConstructorInfo 实例,你遍历这个数组来挑选你需要的数组,通常是找到你需要的签名,你可以看一下参数列表:

http://msdn.microsoft.com/en-us/library/system.reflection.methodbase。 getparameters.aspx [ ^ ]。



当你找到合适的构造函数时,你可以使用invoke方法来创建一个对象:

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 [ ^ ]。



最后,将对象用作必需类型的对象,您需要将其转换为所需类型。



-SA
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


这篇关于如何在运行时创建已知的数据类型实例。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆