如何申报其其类型在C#用户定义的一个实例 [英] How to declare an instance which its type is user-defined in C#
本文介绍了如何申报其其类型在C#用户定义的一个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
只要我的问题是有关定义在运行时的一个实例的类型,该类型是用户定义的。 东西看起来是这样的:
Simply my question is about defining the type of an instance in run time, and this type is user-defined. Something which looks like this:
Type instance1;
在运行时,用户会选择如INT,则会是,
In run time, the user is going to choose for example "int", then there 'll be,
int instance1;
任何建议?在向下转换效率吗?
Any suggestions?? is downCasting efficient here?
推荐答案
你在寻找什么是仿制药。阅读介绍这里。
What you are looking for are generics. Read an introduction here.
例如你声明类,希望该用户定义的类型是,这样的:
For example you would declare the class, you want that user defined type to be in, like that:
class MyClass<T>
{
private T instance1 = default(T);
public MyClass(T initalvalue)
{
instance1 = initalvalue;
}
//... some more code
}
这篇关于如何申报其其类型在C#用户定义的一个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文