[英]Can i extend type cast of common value types (like Int32) with implicid converter to my custom type?
What if i need some kind of reference type wrapper for Int32 (the database Id for example). 如果我需要某种类型的Int32引用类型包装器(例如数据库ID),该怎么办? How can i convert Int32 to my class implicitly?
如何将Int32隐式转换为类?
Create a static implicit converter operator: 创建一个静态隐式转换器运算符:
public class MyClass
{
....
static public implicit operator MyClass(int value)
{
// Create a new instance of your class using the value.
}
...
}
Once created, you can do something like this: 创建完成后,您可以执行以下操作:
MyClass myInstance = 48;
For more information, see MSDN: Using Conversion Operators (C# Programming Guide) 有关更多信息,请参见MSDN: 使用转换运算符(C#编程指南)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.