-3

I have found one solution for this but it seems not working for me( (CrazyCasta answer):

how to get both fields and properties in single call via reflection?

As I have told before I have used the code of CrazyCasta:

interface IGetSettable
{
    public void SetValue(
        Object obj,
        Object value,
        Object[] index);

    public Object GetValue(
        Object obj,
        Object[] index);
}

public class ParameterInfoGS : IGetSettable
{
    protected ParameterInfo pi;

    public ParameterInfoExtra(ParameterInfo _pi)
    {
        pi = _pi;
    }

    public void SetValue(
        Object obj,
        Object value,
        Object[] index) { pi.SetValue(obj, value, index); }

    public Object GetValue(
        Object obj,
        Object[] index) { return pi.GetValue(obj, index); }
}

public class FieldInfoGS : IGetSettable
{
    protected FieldInfo pi;

    public FieldInfoExtra(FieldInfo _pi)
    {
        pi = _pi;
    }

    public void SetValue(
        Object obj,
        Object value,
        Object[] index) {pi.SetValue(obj, value, index);}
    public Object GetValue(
        Object obj,
        Object[] index) {return pi.GetValue(obj, index);}
}

public static class AssemblyExtension
{
    public static IGetSettable[] GetParametersAndFields(this Type t)
    {
        List<IGetSettable> retList = new List<IGetSettable>();

        foreach(ParameterInfo pi in t.GetParameters())
            retList.Add(new ParameterInfoExtra(pi));

        foreach(FieldInfo fi in t.GetFields())
            retList.Add(new FieldInfoExtra(fi));

        return retList.ToArray();
    }
}

I have get a number of errors like ParameterInfo does not contains any GetValue SetValue methods

Community
  • 1
  • 1
curiousity
  • 4,703
  • 8
  • 39
  • 59

1 Answers1

0

I was doing something similar in a previous project of mine. Look at that:

IEnumerable<PropertyInfo> props = from n in typeof(MyClass).GetProperties()
                                  let get = n.GetGetMethod()
                                  let set = n.GetSetMethod()
                                  where get != null && set != null &&
                                        n.DeclaringType == typeof(MyClass)
                                  select n;

foreach (PropertyInfo info in props)
{
    // Code goes here. Something like:
    object obj = info.GetValue(instanceOfMyClass);
}

But eventually I got rid of that code, because it is a bad approach ;)

mDC
  • 116
  • 1
  • 8