简体   繁体   English

C#属性基于选择

[英]C# Properties based on choice

I need a little help i am using a class and want to set the properties based on choice on type int,string and datetime here is my code that i wrote but as my constructor will be confused between public string paramValue and public int? 我需要一点帮助我正在使用一个类,并希望根据int,string和datetime类型的选择设置属性这里是我写的代码但是因为我的构造函数将在公共字符串paramValue和public int之间混淆? paramValue what is the best way to set properties based on choice so only one property can be set a time.Thanks for any suggestion paramValue根据选择设置属性的最佳方法是什么,因此只能设置一个属性。感谢任何建议

public class PassData
{
    private string _ParamName { get; set; }
    private int? _ParamValueInt { get; set; }
    private string _ParamValueString { get; set; }
    private DateTime? _ParamValueDateTime { get; set; }


    public string paramName
    {
        get { return _ParamName; }
        set { _ParamName = value;}
    }

    public string paramValue
    {
        get { return _ParamValueString; }
        set {_ParamValueString = value; }
    }

    public int? paramValue
    {
        get { return _ParamValueInt; }
        set { _ParamValueInt = value; }
    }

    public PassData(string ParamName, int ParamValue)
    {
        paramName = ParamName;
        paramValue = ParamValue;
    }
    public PassData(string ParamName, string ParamValue)
    {
        ParamName = ParamName;
        ParamValueString = ParamValue;
    }
    public PassData(string ParamName, DateTime ParamValue)
    {
        ParamName = ParamName;
        ParamValueDateTime = ParamValue;
    }
}

Basically, you can't have multiple properties on an object that only differ by type. 基本上,您不能在仅按类型不同的对象上拥有多个属性 You have a few options. 你有几个选择。

1) Create a single property that can hold various types: 1)创建一个可以容纳各种类型的属性:

private Object _paramValue;
public Object ParamValue
{
   get { return _paramValue; }
   set {_paramValue= value; }
}

In your setter, you can throw an exception if the value is a type you don't like. 在您的setter中,如果值是您不喜欢的类型,则可以抛出异常。 You'd also have to upcast the result every time you called the getter, making this solution not ideal. 您也不得不在每次调用吸气时间上溯造型的结果,使该解决方案并不理想。 If you want to go this route, I'd suggest making the property an interface , and defining various implementations for the types of data you need. 如果你想走这条路,我建议把属性作为一个接口 ,并为你需要的数据类型定义各种实现。

2) Create a generic class: 2)创建一个通用类:

public class PassData<T>
{
   private T _paramValue;
   public T paramValue
   {
      get { return _paramValue; }
      set {_paramValue= value; }
   }
}

This has the disadvantage of not being able to change the type after the instance is created. 这样做的缺点是在创建实例后无法更改类型。 It was unclear if this was a requirement for you. 目前还不清楚这是否是你的要求。

I like this design as it provides for the possibility of making the constructor for this class private: 我喜欢这个设计,因为它提供了使这个类的构造函数私有的可能性:

public class PassData<T>
{
   private PassData(T value)
   {
      this._paramValue = value;
   }
}

If you did this, you can create overloaded static methods to allow the creation of instances: 如果这样做,您可以创建重载的静态方法以允许创建实例:

public static PassData<String> CreateValue(string value)
{
   return new PassData<String>(value);
}

public static PassData<Int32> CreateValue(Int32 value)
{
   return new PassData<Int32>(value);
}

That way, you can control what types can be created. 这样,您就可以控制可以创建的类型。

Not an answer (in the sense that it does not offer you a way to do what you're trying to do, as Mike Christensen's answer covers it). 不是答案(从某种意义上说,它没有为你提供一种方法来做你正在尝试做的事情,正如Mike Christensen的答案所涵盖的那样)。 I just wanted to get more into why what you are trying to do is not working. 我只是想了解为什么你要做的事情不起作用。 Your expectation for it to work is not unreasonable per se, the issue is that c# is not polymorphic on return values. 你对它的工作期望本身并不合理,问题是c#在返回值上不是多态的。 I think some other languages are, C# is not. 我认为其他一些语言是,C#不是。

ie while in c#, you can do: 即在c#中,您可以这样做:

public void test(int val) {}
public void test(string val) {}
// When you call `test` with either an int or a string, 
// the compiler will know which one to call

you CAN'T do: 你做不到:

public int test() {return 1;}
public string test() {return "1";}
// does not compile. The compiler should look at the call
// site and see what you assign the result of "test()" to
// to decide. But there are various edge cases and it was decided
// to leave this out of the language

Now, the get on string paramValue is functionally equivalent to this scenario. 现在, get on string paramValue在功能上等同于这种情况。 You're trying to get the compiler to decide which paramValue to call based on the return value. 您正试图让编译器根据返回值决定调用哪个paramValue。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM