получить экземпляр класса из информации, содержащейся в MemberInfo в C# WPF

После многих бесплодных поисков я не могу найти решение.

Я хочу сделать что-то вроде этогоObject obj = memberInfo.GetInstance();

Вот метод:

              public static void DefineControl(Window currentForm, Role role)
        {
            foreach (MemberInfo member in currentForm.GetType().GetMembers())
            {
                Attribute ? attribute = member.GetCustomAttribute<AccessCategory>();
                if (attribute != null)
                {
                    bool authorized = ((AccessCategory)attribute).Authorized(role.Categories.ToArray());
                    if (!authorized)
                    {

                        Type type = currentForm.GetType();
                        FieldInfo field = type.GetField(member.Name);
                        Control control = (Control)field.GetValue(member); // control is null <- problem here
                        control.IsEnabled = false;
                    }
                }
            }
        }

Я не могу получить экземплярmember

Спасибо за помощь!

А.

1 ответ

Поскольку это свойство, вам придется использоватьPropertyInfoвместоFieldInfo

Грубое решение..

      internal class Program
{
    static void Main(string[] args)
    {
        Button button1 = new Button();
        Type t = button1.GetType();

        var MemberInfo = t.GetMembers();
        foreach (var member in MemberInfo)
        {
            if (member.Name == "IsEnable")
            {
                //set IsEnable Property to true
                ((PropertyInfo)member).SetValue(button1, true);
            }
        }

        //temp.Configure();
    }
}
class Button
{
    public bool IsEnable { get; set; }
}
Другие вопросы по тегам