如何解决将FieldInfo转换为PropertyInfo,反之亦然
我有一个Component target
,string propertyName
,FieldInfo mFieldInfo
和PropertyInfo mPropertyInfo
的课程,其中mPropertyInfo = target.GetType().GetProperty(property);
和mFieldInfo = target.GetType().GetField(property);
,我使用其中的一个变量,该变量不能为null。我只想使用一个变量mProperty
,可以通过PropertyInfo OR FieldInfo进行设置。我不能在所有地方都使用属性,因为我的代码也从我的类中获得属性和字段。
解决方法
我使用这些扩展名来获取其中一个:
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,nameof(member));
}
}
public static void SetValue<T>(this MemberInfo member,T value) => member.SetValue(destObject,(object)value);
,
您可以使用两者的基本类型创建属性:
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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。