将FieldInfo转换为PropertyInfo,反之亦然

时间:2020-08-12 17:57:26

标签: c#

我有一个Component targetstring propertyNameFieldInfo mFieldInfoPropertyInfo mPropertyInfo的课程,其中mPropertyInfo = target.GetType().GetProperty(property);mFieldInfo = target.GetType().GetField(property);,我使用其中的一个变量,该变量不能为null。我只想使用一个变量mProperty,可以通过PropertyInfo OR FieldInfo进行设置。我不能在所有地方都使用属性,因为我的代码也从我的类中获得属性和字段。

2 个答案:

答案 0 :(得分:0)

我使用这些扩展名来获取其中一个:

public static List<MemberInfo> GetPropertiesOrFields(this Type t, BindingFlags bf = BindingFlags.Public | BindingFlags.Instance) =>
    t.GetMembers(bf).Where(mi => mi.MemberType == MemberTypes.Field || mi.MemberType == MemberTypes.Property).ToList();

public static List<string> GetPropertyOrFieldNames(this Type t) => t.GetPropertiesOrFields().Select(mi => mi.Name).ToList();

public static MemberInfo GetPropertyOrField(this Type t, string memberName, BindingFlags bf = BindingFlags.Public | BindingFlags.Instance) =>
    t.GetMember(memberName, bf).Where(mi => mi.MemberType == MemberTypes.Field || mi.MemberType == MemberTypes.Property).Single();

然后使用以下方法来操纵MemberInfo

// ***
// *** MemberInfo Extensions
// ***
public static Type GetMemberType(this MemberInfo member) {
    switch (member) {
        case FieldInfo mfi:
            return mfi.FieldType;
        case PropertyInfo mpi:
            return mpi.PropertyType;
        case EventInfo mei:
            return mei.EventHandlerType;
        default:
            throw new ArgumentException("MemberInfo must be if type FieldInfo, PropertyInfo or EventInfo", nameof(member));
    }
}

public static bool GetCanWrite(this MemberInfo member) {
    switch (member) {
        case FieldInfo mfi:
            return true;
        case PropertyInfo mpi:
            return mpi.CanWrite;
        default:
            throw new ArgumentException("MemberInfo must be if type FieldInfo or PropertyInfo", nameof(member));
    }
}

public static object GetValue(this MemberInfo member, object srcObject) {
    switch (member) {
        case FieldInfo mfi:
            return mfi.GetValue(srcObject);
        case PropertyInfo mpi:
            return mpi.GetValue(srcObject);
        case MethodInfo mi:
            return mi.Invoke(srcObject, null);
        default:
            throw new ArgumentException("MemberInfo must be of type FieldInfo, PropertyInfo or MethodInfo", nameof(member));
    }
}
public static T GetValue<T>(this MemberInfo member, object srcObject) => (T)member.GetValue(srcObject);

public static void SetValue(this MemberInfo member, object destObject, object value) {
    switch (member) {
        case FieldInfo mfi:
            mfi.SetValue(destObject, value);
            break;
        case PropertyInfo mpi:
            mpi.SetValue(destObject, value);
            break;
        case MethodInfo mi:
            mi.Invoke(destObject, new object[] { value });
            break;
        default:
            throw new ArgumentException("MemberInfo must be of type FieldInfo, PropertyInfo or MethodInfo", nameof(member));
    }
}
public static void SetValue<T>(this MemberInfo member, object destObject, T value) => member.SetValue(destObject, (object)value);

答案 1 :(得分:0)

您可以使用两者的基本类型创建属性:

MemberInfo mProperty { get; set; }

然后分配:

mProperty = target.GetType().GetProperty(property);

还有:

mProperty = target.GetType().GetField(property);

另请参阅:

https://docs.microsoft.com/en-us/dotnet/api/system.reflection.memberinfo?view=netcore-3.1#properties