mardi 27 novembre 2018

Getting a PropertyInfo.Name from a FieldInfo object

I have a method which return some FieldInfo objects:

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?





Aucun commentaire:

Enregistrer un commentaire