C# 具有不同 set 和 get 类型的访问器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/971747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accessor with different set and get types?
提问by Nick
Simple question, hopefully a simple answer:
简单的问题,希望有一个简单的答案:
I'd like to do the following:
我想做以下事情:
private DateTime m_internalDateTime;
public var DateTimeProperty
{
get { return m_internalDateTime.ToString(); } // Return a string
set { m_internalDateTime = value; } // here value is of type DateTime
}
The above is just an example of what I'm trying to do. I'd like to have a public accessor to an internal variable of type x. I want the get that variable as a string, but set it using something of type x.
以上只是我正在尝试做的一个例子。我想要一个公共访问器来访问类型为 x 的内部变量。我希望将该变量作为字符串获取,但使用 x 类型的内容设置它。
Is this possible?
这可能吗?
--edit--
- 编辑 -
I just realized I could do something like:
我刚刚意识到我可以做这样的事情:
private DateTime m_internalDateTime;
public object DateTimeProperty
{
get { return m_internalDateTime.ToString(); } // Return a string
set { m_internalDateTime = (DateTime)value; } // here value is of type DateTime
}
But then, let say I use type y instead of a "string" as my 'get' type. If I want to use "DateTimeProperty" else where in my code, I'd have to cast it.
但是,假设我使用类型 y 而不是“字符串”作为我的“获取”类型。如果我想在我的代码中的其他地方使用“DateTimeProperty”,我必须将它强制转换。
采纳答案by Michael Haren
No. You can obviously add the .ToString() in the calling code, but you can't do what you propose without different names like this:
不。您显然可以在调用代码中添加 .ToString() ,但是如果没有像这样的不同名称,您就无法执行您的建议:
private DateTime m_internalDateTime;
public DateTime SetDateTime { set { m_internalDateTime = value; } }
public string GetDateTime { get { return m_internalDateTime.ToString(); } }
Or, even better to use methods instead of properties (as noted in the comments):
或者,甚至更好地使用方法而不是属性(如评论中所述):
private DateTime m_internalDateTime;
public void SetDateTime(DateTime dateTime) { m_internalDateTime = dateTime; }
public string GetDateTime() { return m_internalDateTime.ToString(); }
Keep in mind that var
is for implicitly, compile-time typed variables, not dynamicvariables.
请记住,var
是隐式,编译时类型VARiables,而不是动态的变量。
Definitely do notdo what you noted in your edit. It introduced a break in convention, possible performance implications (albeit slight), and significant localization problems.
绝对不要做你在编辑中提到的。它引入了对惯例的打破、可能的性能影响(尽管很轻微)和严重的本地化问题。
回答by Timothy Carter
As a property, no this is not possible. You could make Get and Set methods that are of different types, but for a property the types must be the same.
作为财产,不,这是不可能的。您可以创建不同类型的 Get 和 Set 方法,但对于属性,类型必须相同。
EDIT:
编辑:
While:
尽管:
private DateTime m_internalDateTime;
public object DateTimeProperty
{
get { return m_internalDateTime.ToString(); } // Return a string
set { m_internalDateTime = (DateTime)value; } // here value is of type DateTime
}
is syntactically correct, will compile and allows you to accept DateTime as input and return a string, this would not be a good plan. It works, but it makes you and anyone accessing this code, perform unneeded validation. Additionally, it is vulnerable to another developer in the future, not knowing, or realizing the implicit rules, for which you have lost compile time safety. Additionally, its hardly any more code to create either two properties, or two methods that accomplish the same goal, in a strongly typed manner.
语法正确,将编译并允许您接受 DateTime 作为输入并返回一个字符串,这不是一个好的计划。它有效,但它会使您和任何访问此代码的人执行不需要的验证。此外,它在未来容易受到其他开发人员的攻击,他们不知道或意识到隐含规则,您已经失去了编译时安全性。此外,几乎没有更多代码可以以强类型的方式创建两个属性或实现相同目标的两个方法。
Personally, I would recommend using two methods (see Jeff Yates comment for a good explanation as to why).
就个人而言,我建议使用两种方法(请参阅 Jeff Yates 评论以了解原因)。
private DateTime m_internalDateTime;
public string GetDateTime()
{
return m_internalDateTime.ToString();
}
public void SetDateTime(DateTime dateTime)
{
m_internalDateTime = dateTime;
}
回答by TheMissingLINQ
Not that way, but you can certainly have a second property that accesses the m_internalDateTime field.
不是那样,但您当然可以拥有访问 m_internalDateTime 字段的第二个属性。
public string DateTimeString
{
get { return m_internalDateTime.ToString(); }
}
回答by kay.one
Simple answer no, to your outside code your property will behave the exact way that a field would, you can't have a property having different set/get types just as you couldn't have a filed be set with a type and when you request it's value get a different type back.
简单的回答不,对于您的外部代码,您的属性的行为方式将与字段完全相同,您不能拥有具有不同设置/获取类型的属性,就像您不能使用类型设置字段一样请求它的值得到一个不同的类型。
回答by Charles Bretana
how about:
怎么样:
private DateTime intDT;
public string DateTimeProperty
{
get { return intDT.ToString(); } // Return a string
set
{
DateTime dt;
if (DateTime.TryParse(value, out dt))
intDT = dt;
else throw new ArgumentException(string.Format(
"{0} cannot be converted to a DateTime.", value);
}
}
回答by Lukasz
Maybe that helps
也许这有帮助
public class TDecimal
{
private decimal? m_value;
public bool HasValue { get { return m_value.HasValue; } }
public decimal Value { get { return m_value.Value; } }
public static implicit operator TDecimal(string a_value)
{
decimal d;
if (decimal.TryParse(a_value, out d))
{
return new TDecimal() {m_value = d};
}
return new TDecimal() {m_value = null};
}
public static implicit operator decimal(TDecimal a_value)
{
if(a_value.HasValue)
{
return a_value.Value;
}
throw new ArgumentNullException("a_value");
}
}
public class A
{
public TDecimal Prop { get; set; }
}
A a = new A();
a.Prop = "123";
if (a.Prop.HasValue)
{
decimal d = a.Prop;
}