ambiguous/conflicting constructors in generic class

前端 未结 5 939
小鲜肉
小鲜肉 2021-01-07 01:54

I\'ve got a generic class:

public class BaseFieldValue
{
    public BaseFieldValue()
    {
        //...
    }

    public BaseFieldValue(string val         


        
相关标签:
5条回答
  • 2021-01-07 02:22

    Couldn't make Type Contraints do what I wanted, so my workaround is removing the ambiguous constructor while retaining the special case for string:

    public class BaseFieldValue<T>
    {
        public BaseFieldValue()
        {
            //...
        } 
    
        public BaseFieldValue(T value) 
        {
            //however many things you need to test for here
            if (typeof(T) == typeof(string))
            {
                SpecialBaseFieldValue(value.ToString());
            }
            else
            {
                //everything else
            }
            //...
        }
    
        private void SpecialBaseFieldValue(string value)
        {
            //...
        }
    
    }
    
    0 讨论(0)
  • 2021-01-07 02:28

    I would probably make one of the overloads into a factory method:

    public static BaseFieldValue<T> Parse(string value){}
    
    0 讨论(0)
  • 2021-01-07 02:38

    A nasty hack, but probably no worse than any of the alternatives:

    public class BaseFieldValue<T>
    {
        public BaseFieldValue()
        {
            // ...
        }
    
        public BaseFieldValue(StringWrapper value)
        {
            // ...
        }
    
        public BaseFieldValue(T value)
        {
            // ...
        }
    
        public class StringWrapper
        {
            private string _value;
    
            public static implicit operator string(StringWrapper sw)
            {
                return sw._value;
            }
    
            public static implicit operator StringWrapper(string s)
            {
                return new StringWrapper { _value = s };
            }
        }
    }
    

    And now it can be used as you need:

    // call the generic constructor
    var myValue = new BaseFieldValue<string>("hello");
    
    // call the string constructor
    var myValue = new BaseFieldValue<int>("hello");
    
    0 讨论(0)
  • 2021-01-07 02:42

    You could do the following:

    public class BaseFieldValue<T>
    {
        public struct Special
        {
            internal string m_value;
            public Special(string value)
            {
                m_value = value;
            }
        }
    
        public BaseFieldValue()
        {
            //...
        }
    
        public BaseFieldValue(Special value)
        {
            //...
        }
    
        public BaseFieldValue(T value)
        {
            //...
        }
    }
    

    ... or, you could add an extra ignored boolean parameter to your special constructor, just to disambiguate it.

    0 讨论(0)
  • 2021-01-07 02:44

    May be you can try thi:

    var myValue = new BaseFieldValue<Object>("hello" as Object);  
    
    0 讨论(0)
提交回复
热议问题