C#属性。多个SET输入类型

时间:2011-07-19 21:22:58

标签: c# properties operator-overloading set

我有一个看起来像这样的属性

private int clientID;

    public int ClientID
    {
        get { return clientID; }
        set { clientID = value; }
    }

我希望能够将字符串传递给此属性,以便setter将它转换为我。我该怎么做?

我知道如何进行转换,我只需要知道如何在不抛出类型错误的情况下传入字符串。

谢谢,

汤姆

9 个答案:

答案 0 :(得分:8)

基本上,你不能。选项:

  • 将ClientID的第二个属性作为文本(由同一字段支持)
  • 有一个单独的方法(例如SetClientID(string)
  • 将属性类型更改为object,并根据传入的值执行不同的操作(urgh - 请不要这样做!)

答案 1 :(得分:3)

你做不到。你可以创建一个像这样的方法

public void SetClientId(string clientId)
{
    this.ClientID = Convert.ToInt32(clientId);
}

答案 2 :(得分:2)

我建议你不要试试。你只会在以后引起头痛。

添加一个额外的setter,而不是......

public string ClientIDText
{
    set
    {
        clientID = int.Parse(value);
    }
}

或创建一个SetClientID(字符串)方法。

创建SetClientID方法有一个好处;你可以创建重载,所以你可以创建

SetClientID(int)
SetClientID(string)
etc.

但是,我仍然认为您在应用中存在歧义,以方便保存几行代码。

答案 3 :(得分:2)

或者,您可以提供一个ClientID类,它在自身,int和string之间进行隐式转换,然后您可以拥有一个属性:

public ClientIdType ClientId
{
    get; set;
}

但是所有的来电者都可以这样使用它:

var myClass = new MyClass();
myClass.ClientId = 1;
myClass.ClientId = "2";

int id = myClass.ClientId;

http://msdn.microsoft.com/en-us/library/85w54y0a.aspx

然而,这似乎只是为了让来电者更容易,但它仍然是一种选择。

答案 4 :(得分:1)

您不能将字符串传递给声明为整数的属性。你可以改用一种方法。

答案 5 :(得分:1)

我认为最干净的解决方案是,正如其他人所建议的那样,是创建一个ClientId类。我认为这不一定是坏事,因为this帖子以一种非常好的方式解释:

  

许多类都倾向于消耗或暴露原始值   像整数和字符串。虽然存在任何这种原始类型   平台,他们倾向于导致程序代码。而且他们经常   通过允许分配无效值来打破封装。

如果需要,有一个单独的类可以为您提供不同的验证id的可能性。最后,它完全是封装。

这样的事情应该让你开始:

//auto-property for your class
public ClientId ClientID
{
   get; set;
}

ClientId类:

public class ClientId
{
  private int _id;

  public ClientId(int id) { _id = id; }
  public ClientId(string id) { //convert id to int, throw exception if invalid }

  public int Value { return _id; }
}

不要忘记实现Equals和GetHashCode,这可以为这个类简单地完成。

答案 6 :(得分:0)

您不能重载属性,因此无效。

你能做的是

  1. 在设置之前将其转换为int(首选)
  2. 创建一种从客户端进行转换的方法,即SetClientIdFromString(strign id )

答案 7 :(得分:0)

作为物业的替代品如何:

    private int rowValue_;
    public object RowValue
    {
        get
        {
            return this.rowValue_;
        }
        set
        {
            var type = value.GetType();
            if (type == typeof(string))
            {
                this.rowValue_ = Convert.ToInt32(value);
            }
            else if (type == typeof(int))
            {
                this.rowValue_ = (int)value;
            }
            else
            {
                throw new InvalidDataException(
                    "HandwritingData RowValue can only be string or int. 
                    Passed in parameter is typeof {0}",
                    value.GetType().ToString());
            }
        }
    }

它对我有用,也可以捕获不良参数。

此致 克里斯

答案 8 :(得分:0)

在这个示例How Can I inherit the string class?中,René有一个很好的贡献,如果你能够拥有一个不是纯字符串的属性 - 但作为一个并且能够投射:

class Foo {
    readonly string _value;
    public Foo(string value) {
        this._value = value;
    }
    public static implicit operator string(Foo d) {
        return d.ToString();
    }
    public static implicit operator Foo(string d) {
        return new Foo(d);
    }
}