简体   繁体   English

从FieldInfo对象获取PropertyInfo.Name

[英]Getting a PropertyInfo.Name from a FieldInfo object

I have a method which uses some FieldInfo objects: 我有一个使用一些FieldInfo对象的方法:

public static T SetFieldValue<T>(this T src, string propName, object value)
            {
                Type type = typeof(T);
                FieldInfo propInfo = type.GetField(propName, 
                    BindingFlags.Instance | BindingFlags.NonPublic);
                Type propType = propInfo.FieldType;
                var targetType = IsNullableType(propType) 
                    ? Nullable.GetUnderlyingType(propType) : propType;
                value = Convert.ChangeType(value, targetType);
                propInfo.SetValue(src, value);
                return src;
            }

Now I need some way of getting a PropertyInfo object from a specific FieldInfo this function returns, mainly because I specifically need the PropertyInfo.Name string, any thoughts on that? 现在,我需要某种方法从此函数返回的特定FieldInfo中获取PropertyInfo对象,这主要是因为我特别需要PropertyInfo.Name字符串,对此有何想法?

Edit: Here's some more specific code, I use this method to get the difference between two objects: 编辑:这是一些更具体的代码,我使用此方法来获取两个对象之间的区别:

public static List<Variance> DetailedCompare<T>(this T val1, T val2)
        {
            List<Variance> variances = new List<Variance>();
            FieldInfo[] fi = typeof(T).GetFields(BindingFlags.Instance | BindingFlags.NonPublic);
            foreach (FieldInfo f in fi) {
                Variance v = new Variance();
                v.Prop = f.Name;
                v.valA = f.GetValue(val1);
                v.valB = f.GetValue(val2);
                if (v.valA != null) {
                    if (!v.valA.Equals(v.valB))
                        variances.Add(v);
                } else if (v.valB != null) {
                    variances.Add(v);
                }
            }
            return variances;
        }

Then I use the returned fields to format a SQL query which I'll send to a webservice, to be more specific. 然后,我使用返回的字段来格式化SQL查询,该查询将发送到Web服务,更具体地说。 But the FieldInfo.Name objects are generally something like k__BackingField or k__BackingField I need the PropertyInfo.Name, which is the correct name. 但是FieldInfo.Name对象通常类似于k__BackingField或k__BackingField,我需要PropertyInfo.Name,这是正确的名称。 Is there some way to create a PropertyInfo object from a FieldInfo object? 有什么方法可以从FieldInfo对象创建PropertyInfo对象?

i got this in my old codes. 我在旧代码中得到了这个。

    private static PropertyInfo GetInfo(object myObject, string name)
    {
        return myObject.GetType().GetProperty(name);
    }

you can use PropertyInfo 您可以使用PropertyInfo

msdn https://docs.microsoft.com/en-us/dotnet/api/system.reflection.propertyinfo?view=netframework-4.7.2 msdn https://docs.microsoft.com/zh-cn/dotnet/api/system.reflection.propertyinfo?view=netframework-4.7.2

and if you want get all members in T 如果您想让所有T成员

            Type type = typeof(T);
            MemberInfo[] tempInfo = type.GetMembers();

MemberInfo https://docs.microsoft.com/en-us/dotnet/api/system.reflection.memberinfo?view=netframework-4.7.2 MemberInfo https://docs.microsoft.com/zh-cn/dotnet/api/system.reflection.memberinfo?view=netframework-4.7.2

I made my way around it: 我绕过它:

public static List<Variance> DetailedCompare<T>(this T val1, T val2, bool pName = false)
        {
            List<Variance> variances = new List<Variance>();
            FieldInfo[] fi = typeof(T).GetFields(BindingFlags.Instance | BindingFlags.NonPublic);
            foreach (FieldInfo f in fi) {
                Variance v = new Variance();
                PropertyInfo prop = default(PropertyInfo);
                if (pName) {
                    prop = typeof(T).GetProperties().Where(p => f.Name.Contains(p.Name)).FirstOrDefault();
                }
                v.Prop = pName ? (prop != default(PropertyInfo) ? prop.Name : "") : f.Name;
                v.valA = f.GetValue(val1);
                v.valB = f.GetValue(val2);
                if (v.valA != null) {
                    if (!v.valA.Equals(v.valB))
                        variances.Add(v);
                } else if (v.valB != null) {
                    variances.Add(v);
                }
            }
            return variances;
        }

What I did is giving the possibility of this method returning the PropertyInfo.Name. 我所做的就是让该方法返回PropertyInfo.Name的可能性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM