泛型类中的模糊/冲突构造函数 [英] ambiguous/conflicting constructors in generic class
本文介绍了泛型类中的模糊/冲突构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个泛型类:
public class BaseFieldValue< T>
{
public BaseFieldValue()
{
// ...
}
public BaseFieldValue(string value)
{
// ...
}
public BaseFieldValue(T值)
{
// ...
}
}
好。除了......
var myValue = new BaseFieldValue< string>(hello);
糟糕。不需要的构造函数被调用。有很多方法可以解决这个问题。什么是最好的解决方案?
解决方案我可能会将其中一个重载转换为工厂方法:
public static BaseFieldValue< T>解析(字符串值){}
I've got a generic class:
public class BaseFieldValue<T>
{
public BaseFieldValue()
{
//...
}
public BaseFieldValue(string value)
{
//...
}
public BaseFieldValue(T value)
{
//...
}
}
Fine. Except...
var myValue = new BaseFieldValue<string>("hello");
Oops. The undesired constructor is called. There's a number of ways to address the problem. What's the best solution?
解决方案
I would probably make one of the overloads into a factory method:
public static BaseFieldValue<T> Parse(string value){}
这篇关于泛型类中的模糊/冲突构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文