c#Reflection - 访问静态类的对象属性

时间:2016-02-02 17:25:11

标签: c# reflection

我使用this post中的代码来实现应用程序中的功能切换,并且它运行良好。但是,现在不是我的功能是直接bool开/关,每个功能都有两个bool值:一个用于常规设置,另一个用于设置,仅适用于用户子集。我一直在尝试更新代码来处理这个问题,而且我发现我对反射和访问器的基本理解已经很短了。

我正在使用下面的代码在LINQPad中进行测试 - 基本上,与上面提到的帖子相比,我所做的一切都是尝试从bool更改TestFeatureToggle,输入MultiToggle但是我很难设置或获取MultiToggle对象的bool属性的值。

我得到一个例外
return (bool)multiToggleProperty.GetValue(null,null);

“非静态方法需要目标”。这是有道理的,因为MultiToggle上的属性是非静态的。但是,我没有这些对象的实例,我不明白如何从静态类中获取它。

任何帮助都会非常感激。我的直觉说这可能不可能!

        void Main()
    {
        var exampleFeatureToggles = new List<FeatureToggle>
        {
            new FeatureToggle {Description = "Test", Id = 1, IsActive = true, Name = "TestFeatureToggle"}
        };

        FeatureToggles.SetFeatureToggles(exampleFeatureToggles);

        Console.WriteLine(FeatureToggles.GetFeatureToggleSetting("TestFeatureToggle"));
    }

    public class FeatureToggle
    {
        public string Description { get; set; }
        public int Id { get; set; }
        public bool IsActive { get; set; }
        public string Name { get; set; }
    }

    public class MultiToggle
    {

        public bool DefaultValue { get; private set; }
        public bool OtherValue { get; private set; }

    }

    public static class FeatureToggles
    {

        //public static bool TestFeatureToggle { get; private set; }
        public static MultiToggle TestFeatureToggle { get; private set; }

        public static void SetFeatureToggles(List<FeatureToggle> toggles)
        {
            if (toggles == null) return;

            var properties = typeof(FeatureToggles).GetProperties(BindingFlags.Public | BindingFlags.Static);

            // All properties must be set to false to prevent a property staying true when deleted from the database
            foreach (var property in properties)
            {
                var multiToggleProperties = typeof(MultiToggle).GetProperties();
                foreach (var multiToggleProperty in multiToggleProperties)
                {
                    multiToggleProperty.SetValue(new MultiToggle(), false, null);
                }

            }

            foreach (var toggle in toggles)
            {
                foreach (var property in properties)
                {
                    if (property.Name.ToLower() == toggle.Name.ToLower())
                    {
                        Type tProp = property.GetType();
                        var multiToggleProperties = typeof(MultiToggle).GetProperties();

                        foreach (var multiToggleProperty in multiToggleProperties)
                        {
                            Console.WriteLine(multiToggleProperty);
                            multiToggleProperty.SetValue(new MultiToggle(), toggle.IsActive, null);
                        }
                    }
                }
            }

        }

        public static bool GetFeatureToggleSetting(string propertyName)
        {

            var properties = typeof(FeatureToggles).GetProperties(BindingFlags.Public | BindingFlags.Static);

            foreach (var property in properties)
            {
                if (property.Name.ToLower() == propertyName.ToLower())
                {
                    Type tProp = property.GetType();
                    var multiToggleProperties = typeof(MultiToggle).GetProperties();
                    Console.WriteLine(multiToggleProperties);
                    foreach (var multiToggleProperty in multiToggleProperties)
                    {
                        Console.WriteLine(multiToggleProperty);
                        return (bool)multiToggleProperty.GetValue(null, null);
                    }
                }
            }

            return false;

        }
    }

1 个答案:

答案 0 :(得分:1)

这个想法在那里,但你基本上“只是超越了标记”。您正在获取“非静态方法需要目标”错误,因为您试图获取静态属性值中的属性值,而不首先获取静态属性的值(多么令人满意)。即你要去:

get static property type -> get instance property type -> get value of property from static property.

DefaultValueOtherValue是实例属性,因此在获得其值之前,您首先需要实例对象。我做了一些调整,只是为了向您展示如何设置静态属性并从静态属性中获取值。你应该可以从那里调整它:

void Main()
{
    var exampleFeatureToggles = new List<FeatureToggle>
    {
        new FeatureToggle {Description = "Test", Id = 1, IsActive = true, Name = "TestFeatureToggle"}
    };

    FeatureToggles.SetFeatureToggles(exampleFeatureToggles);

    Console.WriteLine(FeatureToggles.GetFeatureToggleSetting("TestFeatureToggle"));
}

public class FeatureToggle
{
    public string Description { get; set; }
    public int Id { get; set; }
    public bool IsActive { get; set; }
    public string Name { get; set; }
}

public class MultiToggle
{
    public bool DefaultValue { get; private set; }
    public bool OtherValue { get; private set; }
}

public static class FeatureToggles
{
    //public static bool TestFeatureToggle { get; private set; }
    public static MultiToggle TestFeatureToggle { get; private set; }

    public static void SetFeatureToggles(List<FeatureToggle> toggles)
    {
        if (toggles == null) return;

        var properties = typeof(FeatureToggles).GetProperties(BindingFlags.Public | BindingFlags.Static);

        // All properties must be set to false to prevent a property staying true when deleted from the database
        foreach (var property in properties)
        {
            // first change: set the _property_, not multiToggleProperty
            property.SetValue(null, new MultiToggle());
        }

        foreach (var toggle in toggles)
        {
            foreach (var property in properties)
            {
                if (property.Name.ToLower() == toggle.Name.ToLower())
                {
                    Type tProp = property.GetType();
                    var multiToggleProperties = typeof(MultiToggle).GetProperties();

                    // second change: create a nee instance, set the values, then set that value on the static property
                    var newMultiToggle = new MultiToggle();
                    property.SetValue(null, newMultiToggle);
                    foreach (var multiToggleProperty in multiToggleProperties)
                    {
                        Console.WriteLine(multiToggleProperty);
                        multiToggleProperty.SetValue(newMultiToggle, toggle.IsActive, null);
                    }
                }
            }
        }
    }

    public static bool GetFeatureToggleSetting(string propertyName)
    {
        var properties = typeof(FeatureToggles).GetProperties(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);

        foreach (var property in properties)
        {
            if (property.Name.ToLower() == propertyName.ToLower())
            {
                Type tProp = property.GetType();

                // third change: get the static value first, then get the value from the properties on that instance.
                var value = property.GetValue(null);
                var multiToggleProperties = typeof(MultiToggle).GetProperties();
                Console.WriteLine(multiToggleProperties);
                foreach (var multiToggleProperty in multiToggleProperties)
                {
                    Console.WriteLine(multiToggleProperty);
                    return (bool)multiToggleProperty.GetValue(value, null); //
                }
            }
        }

        return false;
    }
}