Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何引用对象中的属性以每次重新评估它们_C#_Properties - Fatal编程技术网

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替换它们,这是您要求的;)@你说的一行是什么意思?私人方法?