C# 属性设置器中的多种输入类型
我有一个如下所示的属性:
private int clientID;
public int ClientID
{
get { return clientID; }
set { clientID = value; }
}
我希望能够将字符串传递给该属性,以便设置器为我转换它。我该怎么做?
我知道如何进行转换,我只需要知道如何传入字符串而不引发类型错误。
I have a property that looks like this:
private int clientID;
public int ClientID
{
get { return clientID; }
set { clientID = value; }
}
I would like to be able to pass in a string to this property so that the setter will convert it for me. How do I do that?
I know how to do the conversion, I just need to know how to pass in a string without it throwing a type error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
基本上你不能。选项:
SetClientID(string)
)object
并根据传入的值做不同的事情(呃 - 请不要这样做!)You can't, basically. Options:
SetClientID(string)
)object
and do different things based on the value passed in (urgh - please don't do this!)你不能。不过,您可以创建这样的方法
You cannot. You can create a method like this though
您不能将字符串传递给声明为整数的属性。您可以改用一种方法。
You can't pass a string to a property which is declared as integer. You could use a method instead.
我建议你不要尝试。你只会给以后的约会带来麻烦。
添加一个额外的 setter,
或者创建一个 SetClientID(string) 方法。
创建 SetClientID 方法有一个优点;您可以创建重载,因此您可以创建
但是,我仍然认为您为了方便保存几行代码而在应用程序中构建了歧义。
I recommend you don't try. You're only causing headaches for a later date.
Add an additional setter, instead...
or create a SetClientID(string) method.
There is an advantage to create a SetClientID method; you can create overloads, so you could create
However, I still think you're building ambiguity into your app for the convenience of saving a few lines of code.
或者,您可以提供一个 ClientID 类,该类在自身、int 和 string 之间进行隐式转换,然后您可以拥有一个属性:
但所有调用者都可以这样使用它:
http://msdn.microsoft.com/en-us/library/85w54y0a.aspx
但是,这似乎是大量的跑腿工作只是为了让呼叫者更容易,但这仍然是一个选择。
Alternatively, you can provide a ClientID class that has implicit conversion between itself, int and string, then you can have a property:
But all the callers could then use it thus:
http://msdn.microsoft.com/en-us/library/85w54y0a.aspx
However, this seems like a lot of legwork just to make it easier for the caller, but it's still an option.
我认为最干净的解决方案是,正如其他人建议的那样,创建一个 ClientId 类。我认为这不一定是坏事,正如 这篇 文章中所解释的那样一个非常好的方法:
如果需要的话,拥有一个单独的类也可以为您提供不同类型的验证 id 的可能性。最后,一切都与封装有关。
像这样的事情应该可以帮助您开始:
还有 ClientId 类:
不要忘记实现 Equals 和 GetHashCode,对于此类来说这可以轻松完成。
I think the cleanest solution is, as some others have suggested is to create a ClientId class. I don't think this is necessarily bad, as this post explains in a pretty good way:
Having a seperate class gives you different sorts of possibilities to validate the the id as well, if that is a requirement. In the end, it is all about encapsulation.
Something like this should get you started:
And the ClientId class:
Don't forget to implement Equals and GetHashCode as well, which can be done trivially for this class.
您不能重载属性,因此这是行不通的。
您可以做的就是
SetClientIdFromString(strign id )
。You can't overload properties, so that won't work.
What you can do is
SetClientIdFromString(strign id )
.作为属性中的替代方案怎么样:
它对我有用,并且可以捕获错误的参数。
问候,
克里斯
How about as an alternative in the property:
It works for me as well as trapping bad parameters.
Regards,
Chris
在此示例中 如何继承 string 类? 有一个如果您可以忍受拥有一个不是纯字符串的属性,但可以作为一个属性并且能够 投掷:
In this example How Can I inherit the string class? there is a nice contribution by René if you can stand having a property not being a pure string - but serve as one and be able to cast: