C# 数据类型的枚举
c# 中是否有包含 c# 数据类型的枚举。这样我就可以在类中定义一个属性,该属性接受用户的数据类型(int,string)。
Is there any enum in c# which holds c# datatypes. So that I can define a property in a class which accepts datatype (int,string) from the user.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
有 TypeCode 枚举系统。看起来它涵盖了所有基本类型。
您可以使用 Type.GetTypeCode() 获取任何对象的 TypeCode:
There is the TypeCode Enumeration in System. Looks like it covers all of the base types.
You can get the TypeCode for any object using Type.GetTypeCode():
您只想将枚举值与字符串关联吗?您可能想要使用
Description
属性。定义映射的另一种可能性是使用
Dictionary
。Do you simply want to associate an enum value with a string? You might want to use the
Description
attribute.Another possibility for defining a mapping would be to use a
Dictionary<int, string>
.Type-type 有一个布尔属性“IsPrimitive”希望这对您有帮助。
There is a boolean property of Type-type "IsPrimitive" hope this helps you.
根据您的编辑,听起来您需要 泛型但我仍然质疑为什么属性可以是 int 或 string。这些确实是非常不同的事情,只能导致向上转型。
Based on your edit sounds like you need generics but I still question why a property would acceptably be an int or a string. Those are really very different things which can only lead to upcasting.
BCL 中没有类似的东西。
为什么需要它?
There is nothing like that in the BCL.
Why do you need it?
你为什么需要那个?该属性已经是它可以接受的数据类型的“过滤器”。
看一下:
在 C# 中重载属性
Why do you need that? The property is already a "filter" to what kind of data it can accept.
Have a look at :
Overloading properties in C#