0

After many fruitless searches I can't find the solution.

I want to do something like this Object obj = memberInfo.GetInstance();

Here is the method:

        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;
                    }
                }
            }
        }

I can't get the instance of member

Thank you for your help!

A.

Aiorio
  • 1
  • 2
  • Why did you get a `MemberInfo`? Shouldn't it be a `FieldInfo`? Then you can just use [`GetValue`](https://learn.microsoft.com/en-us/dotnet/api/system.reflection.fieldinfo.getvalue?view=net-7.0) to get the instance of `Button`. – Sweeper Jul 07 '23 at 09:38
  • Yes, I've tried that, but it doesn't work. – Aiorio Jul 07 '23 at 09:42
  • 2
    What do you mean by "doesn't work"? Please show a [mcve] by [edit]ing your question, – Sweeper Jul 07 '23 at 09:43
  • Are you looking for [Activator.CreateInstance](https://learn.microsoft.com/en-us/dotnet/api/system.activator.createinstance?view=net-7.0)? – nalka Jul 20 '23 at 11:29

1 Answers1

0

As it's a property, you will have to use PropertyInfo instead of FieldInfo

A rough solution..

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; }
}
Sekhar
  • 5,614
  • 9
  • 38
  • 44
  • Ty, your solution set the `IsEnabled` to `true`, but i want to retrieve the instance of the `Button button1` from the `FieldInfo` or from the `MemberInfo` inside a `Control` type then `myRetrievedControl.IsEnabled = true`. – Aiorio Jul 07 '23 at 09:56
  • You'll have to use another method, called GetValue.. I believe ```((PropertyInfo)member).GetValue(button1)``` would work – Sekhar Jul 07 '23 at 09:58
  • Yes, exactly, but i don't have `button1`, i just have a `MemberInfo` or a `FieldInfo` – Aiorio Jul 07 '23 at 10:00
  • is it a Winforms project? or ASP.Net - if you create a new minimal project and send it over I can attempt to reproduce it – Sekhar Jul 07 '23 at 10:02
  • It is a WPF project – Aiorio Jul 07 '23 at 10:10
  • i just edited my first question – Aiorio Jul 07 '23 at 10:14