Similar to a previous question of mine, when I was asking about getting the FieldInfo of a field, How to get the FieldInfo of a field from the value, from the answers there, I compiled this helper class,
using System;
using System.Reflection;
using System.Linq.Expressions;
internal class Program
{
class MyClass
{
#pragma warning disable 0414, 0612, 0618, 0649
private int myInt = 24;
#pragma warning restore 0414, 0612, 0618, 0649
public const BindingFlags _flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
public MyClass()
{
MemberInfo myIntMI = GetMemberInfo(this, c => c.myInt);
Console.WriteLine(myIntMI.Name + ": " + GetFieldValue(myIntMI) + ", " + GetFieldInfo(myIntMI).FieldType);
// MemberInfo tfMI = GetMemberInfo(this, cw => cw.testFunction());
// MemberInfo tfMI = GetMemberInfo(this, cw => cw.testFunction);
// Console.WriteLine(tfMI.Name + ": " + GetFieldValue(tfMI) + ", " + GetFieldInfo(tfMI).FieldType);
foreach( var mi in GetType().GetMembers(_flags) )
{
Console.WriteLine("member: " + mi);
}
}
private void testFunction() { }
private object GetFieldValue(MemberInfo mi)
{
return GetFieldInfo(mi).GetValue(this);
}
private FieldInfo GetFieldInfo(MemberInfo mi)
{
return GetType().GetField(mi.Name, _flags);
}
private MemberInfo GetMemberInfo<TModel, TItem>(TModel model, Expression<Func<TModel, TItem>> expr)
{
return ( (MemberExpression)expr.Body ).Member;
}
}
}
Which works perfectly well using the GetMemberInfo(this, c => c.myInt
, but the commented out line is what I'm confused about now, GetMemberInfo(this, c => c.testFunction)
or GetMemberInfo(this, c => c.testFunction())
.
Is there any way, without string comparison, I can get the member info that's available from a GetMembers()
runthrough, or GetMember("testFunction")
?
Aucun commentaire:
Enregistrer un commentaire