我想我以前在什么地方见过,但现在我想不起来了,也找不到了。有没有一种方法可以让getter属性带有参数?
我的意思是,我可以将"float getSize();“转换为"float Size":
float getSize() {
return this.size;
}
float Size {
get { return this.size; }
}那么,例如,我是否可以将"float getSize(String unit);“转换为"float getSize(String unit)”或类似的内容?
float getSize(String unit) {
return this.size;
}
float Size(String unit) {
get {
if (unit == Unit.Meters)
return this.size/100;
else
return this.size;
}
}我认为使用函数根本没有问题,但这样看起来可能更好:P
发布于 2011-05-22 22:54:29
回答这个问题:不,这是不可能的,正如已经指出的,带有参数的getter看起来就像一个方法。
您正在考虑的事情可能是一个索引的默认属性,如下所示:
class Test
{
public string this[int index]
{
get { return index.ToString(); }
}
}这允许您索引到Test的实例,如下所示:
Test t = new Test();
string value = t[1];发布于 2015-01-26 20:15:40
有趣的是,在VB.NET中可以使用带参数的属性,如下所示:
Public ReadOnly Property oPair(param As String) As Result
Get
'some code depends on param
End Get
End Property它并不优于常规函数,但有时有这样的可能性是很好的。
发布于 2021-04-22 05:31:02
我知道这是一个古老的帖子,但我今天在C#中遇到了想要这样做的想法。不管这是不是一件好事,可能都是不好的一面。然而,我偶然发现了Mark Jones在(https://social.msdn.microsoft.com/forums/en-US/5a25bc83-990e-4657-aa9c-69bca5158d48/overloaded-c-properties-with-arguments?prof=required)上发布的一个有趣的想法,但我不太喜欢它的感觉。
因此,我基于他的(我的是在.Net 5.0中使用Nullable =enable)编写了自己的代码:
class ParameterizedProperty<T>
{
private readonly Func<int, T> getter;
private readonly Action<int, T> setter;
public T this[int index]
{
get => this.getter(index);
set => this.setter(index, value);
}
public ParameterizedProperty(Func<int, T> getter, Action<int, T> setter)
{
this.getter = getter;
this.setter = setter;
}
}
class NamedParameterizedProperty<T>
{
private readonly Func<int, T> indexedGetter;
private readonly Action<int, T> indexedSetter;
private readonly Func<string, T> namedGetter;
private readonly Action<string, T> namedSetter;
public T this[int index]
{
get => this.indexedGetter(index);
set => this.indexedSetter(index, value);
}
public T this[string name]
{
get => this.namedGetter(name);
set => this.namedSetter(name, value);
}
public NamedParameterizedProperty(Func<int, T> indexedGetter, Action<int, T> indexedSetter, Func<string, T> namedGetter, Action<string, T> namedSetter)
{
this.indexedGetter = indexedGetter;
this.indexedSetter = indexedSetter;
this.namedGetter = namedGetter;
this.namedSetter = namedSetter;
}
}
class ReadOnlyParameterizedProperty<T>
{
private readonly Func<int, T> getter;
public T this[int index] => this.getter(index);
public ReadOnlyParameterizedProperty(Func<int, T> getter)
{
this.getter = getter;
}
}
class ReadOnlyNamedParameterizedProperty<T>
{
private readonly Func<int, T> indexedGetter;
private readonly Func<string, T> namedGetter;
public T this[int index] => this.indexedGetter(index);
public T this[string name] => this.namedGetter(name);
public ReadOnlyNamedParameterizedProperty(Func<int, T> indexedGetter, Func<string, T> namedGetter)
{
this.indexedGetter = indexedGetter;
this.namedGetter = namedGetter;
}
}关于我的解决方案:我选择Func<> & Action<>作为getter/setter,因为我不想让这个helper类需要任何关于它所支持的底层属性的知识。相反,拥有该属性的类将拥有用于get_X / set_X (或您希望使用的任何命名约定)的公共(或私有)方法,这些方法将处理所有事情-例如验证。
现在来看我的用例:我有一个类,它有一个特定类型的内部数组。我有一个默认的索引器属性public primaryType this[int index],但它还有一些可以理解的其他类型,并且可以在primaryType中相互转换。但是,我不能做public otherType this[int index],我也不想做像'get_OtherType` & 'set_OtherType‘这样的公共方法。
这些帮助器类让我可以做一些类似的事情:
public ParameterizedProperty<OtherType> OtherType { get; }
public MyClass()
{
this.OtherType = new(get_OtherType, set_OtherType);
}
private OtherType get_OtherType(int index)
{
/* validate index, convert PrimaryType at index to OtherType and return. */
}
private void set_OtherType(int index, OtherType value)
{
/* validate index, validate value, convert to PrimaryType and set to internal array. */
}然后在使用这个类的其他类/UI中,'OtherType‘比'PrimaryType’更便于它们使用,我可以让它们做像myClass1.OtherType[0] = otherType;这样的事情,但如果它们使用主类型,那么它们可以做myClass1[0] = primaryType -或者如果我只是想保持一致/显式,我没有默认的索引器属性,我也对主类型使用ParameterizedProperty,即IE:myClass1.PrimaryType[0] = primaryType;
再说一次,这是不是一个好主意,我不确定。
https://stackoverflow.com/questions/6088660
复制相似问题