C# 如何引用对象中的属性以每次重新评估它们
我尝试使用字典将一些C# 如何引用对象中的属性以每次重新评估它们,c#,properties,C#,Properties,我尝试使用字典将一些enum映射到一些bool属性: private Dictionary<FooEnum, bool> isBarByFoo; private Dictionary<FooEnum, bool> IsBarByFoo { get { return isBarByFoo ?? (isBarByFoo = new Dictionary<FooEnum, bool> { {
enum
映射到一些bool
属性:
private Dictionary<FooEnum, bool> isBarByFoo;
private Dictionary<FooEnum, bool> IsBarByFoo
{
get
{
return isBarByFoo ?? (isBarByFoo = new Dictionary<FooEnum, bool>
{
{ FooEnum.Foo1, IsBar1 },
{ FooEnum.Foo2, IsBar2 },
{ FooEnum.Foo3, IsBar3 }
});
}
}
private bool IsBar1 => SomeConditionsThatChangeOverRuntime1();
private bool IsBar2 => SomeConditionsThatChangeOverRuntime2();
private bool IsBar3 => SomeConditionsThatChangeOverRuntime3();
私人字典isBarByFoo;
私人字典IsBarByFoo
{
得到
{
返回isBarByFoo???(isBarByFoo=新字典
{
{fooneum.Foo1,IsBar1},
{fooneum.Foo2,IsBar2},
{fooneum.Foo3,IsBar3}
});
}
}
private bool IsBar1=>SomeConditionSthatChangeOverflowTime1();
private bool IsBar2=>SomeConditionsThatChangeOverflowTime2();
private bool IsBar3=>SomeConditionsThatChangeOverflowTime3();
但是,这种方法不起作用,因为在创建字典并将其保存为值类型时,只需对属性进行一次计算,结果证明我误解了属性的工作方式
是否可以在字典中存储属性,每次查找时都对其进行评估,或者您可以为此类问题提出不同的解决方案?如何:
private Dictionary<FooEnum, Func<bool>> isBarByFoo;
private Dictionary<FooEnum, Func<bool>> IsBarByFoo
{
get
{
return isBarByFoo ?? (isBarByFoo = new Dictionary<FooEnum, Func<bool>>
{
{ FooEnum.Foo1, SomeConditionsThatChangeOverRuntime1},
{ FooEnum.Foo2, SomeConditionsThatChangeOverRuntime2},
{ FooEnum.Foo3, SomeConditionsThatChangeOverRuntime3}
});
}
}
private bool SomeConditionsThatChangeOverRuntime1() => true;
private bool SomeConditionsThatChangeOverRuntime2() => false;
private bool SomeConditionsThatChangeOverRuntime3() => true;
私人字典isBarByFoo;
私人字典IsBarByFoo
{
得到
{
返回isBarByFoo???(isBarByFoo=新字典
{
{FooEnum.Foo1,someConditionsThatChangeOverflowTime1},
{FooEnum.Foo2,someConditionsThatChangeOverflowTime2},
{FooEnum.Foo3,someConditionsThatChangeOverflowTime3}
});
}
}
private bool someconditionsthatchchangesoverflowtime1()=>true;
private bool someconditionsthatchchangeOverflowTime2()=>false;
private bool someconditions thatChangeOverflowTime3()=>true;
当您通过IsBarByFoo[FooEnum.Foo1]()获取字典值时,您将获得所需的值那么:
private Dictionary<FooEnum, Func<bool>> isBarByFoo;
private Dictionary<FooEnum, Func<bool>> IsBarByFoo
{
get
{
return isBarByFoo ?? (isBarByFoo = new Dictionary<FooEnum, Func<bool>>
{
{ FooEnum.Foo1, SomeConditionsThatChangeOverRuntime1},
{ FooEnum.Foo2, SomeConditionsThatChangeOverRuntime2},
{ FooEnum.Foo3, SomeConditionsThatChangeOverRuntime3}
});
}
}
private bool SomeConditionsThatChangeOverRuntime1() => true;
private bool SomeConditionsThatChangeOverRuntime2() => false;
private bool SomeConditionsThatChangeOverRuntime3() => true;
私人字典isBarByFoo;
私人字典IsBarByFoo
{
得到
{
返回isBarByFoo???(isBarByFoo=新字典
{
{FooEnum.Foo1,someConditionsThatChangeOverflowTime1},
{FooEnum.Foo2,someConditionsThatChangeOverflowTime2},
{FooEnum.Foo3,someConditionsThatChangeOverflowTime3}
});
}
}
private bool someconditionsthatchchangesoverflowtime1()=>true;
private bool someconditionsthatchchangeOverflowTime2()=>false;
private bool someconditions thatChangeOverflowTime3()=>true;
当您通过IsBarByFoo[FooEnum.Foo1]()获取字典值时,您将获得所需的值另一种解决方案可能是忘记字典,只需使用一种方法来获取值:
private bool GetIsBarByFoo(FooEnum foo)
{
switch (foo)
{
case FooEnum.Foo1:
return SomeConditionsThatChangeOverRuntime1();
case FooEnum.Foo2:
return SomeConditionsThatChangeOverRuntime2();
case FooEnum.Foo3:
return SomeConditionsThatChangeOverRuntime3();
default:
return false;
}
}
另一种解决方案可能是忘记字典,只使用一种方法来获取值:
private bool GetIsBarByFoo(FooEnum foo)
{
switch (foo)
{
case FooEnum.Foo1:
return SomeConditionsThatChangeOverRuntime1();
case FooEnum.Foo2:
return SomeConditionsThatChangeOverRuntime2();
case FooEnum.Foo3:
return SomeConditionsThatChangeOverRuntime3();
default:
return false;
}
}
为什么不每次都返回一个新字典呢?为什么不每次都返回一个新字典呢?为什么不每次都返回一个新字典呢?这些属性比我在示例中使用的一行程序更复杂,并且在某些时候可能有setter,所以我不能用methodsWell替换它们,这是您要求的;)@你说的一行是什么意思?私有方法?属性比我在示例中使用的一行程序更复杂,并且可能在某个点上有setter-因此我不能用methodsWell替换它们,这是您要求的;)@你说的一行是什么意思?私人方法?