本文整理汇总了C#中System.Reflection.MemberInfo.Get方法的典型用法代码示例。如果您正苦于以下问题:C# MemberInfo.Get方法的具体用法?C# MemberInfo.Get怎么用?C# MemberInfo.Get使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.MemberInfo
的用法示例。
在下文中一共展示了MemberInfo.Get方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ForMember
public Maybe<object> ForMember(MemberInfo member, Func<object> instanceAccessor)
{
Func<object> propertyAccessor = () => member.Get(instanceAccessor());
var fieldType = member.GetMemberType();
foreach (var builder in Builders.Where(builder => builder.Key(fieldType)))
{
return builder.Value(member, propertyAccessor);
}
return new Nothing<object>();
}
示例2: RegisterMember
private void RegisterMember(Type type, Reflection.MemberInfo m, Type mType, Func<object, object> get, Action<object, object> set)
{
if (mType.IsSubclassOf(typeof(Delegate)))
return;
// struct that holds access method for property/field
MemberInfo accessor = new MemberInfo();
accessor.Type = mType;
accessor.Get = get;
accessor.Set = set;
if(set!=null){ // writeable ?
accessor.SerializeMethod = YamlSerializeMethod.Assign;
} else {
accessor.SerializeMethod = YamlSerializeMethod.Never;
if ( mType.IsClass )
accessor.SerializeMethod = YamlSerializeMethod.Content;
}
var field = m as FieldInfo;
var prop = m as PropertyInfo;
var attr1 = m.GetAttribute<YamlSerializeAttribute>();
if ( attr1 != null ) { // specified
if ( set == null ) { // read only member
if ( attr1.SerializeMethod == YamlSerializeMethod.Assign ||
( mType.IsValueType && accessor.SerializeMethod == YamlSerializeMethod.Content ) )
throw new ArgumentException("{0} {1} is not writeable by {2}."
.DoFormat(mType.FullName, m.Name, attr1.SerializeMethod.ToString()));
}
accessor.SerializeMethod = attr1.SerializeMethod;
}
else
{
//if there are no attributes, and the field/property is private, internal, or protected, set it to not serialize
if (field != null)
{
if (field.IsFamilyOrAssembly || field.IsPrivate)
accessor.SerializeMethod = YamlSerializeMethod.Never;
}
else if (prop != null)
{
if (accessor.Get.Method.IsFamilyOrAssembly || accessor.Get.Method.IsPrivate)
accessor.SerializeMethod = YamlSerializeMethod.Never;
}
}
if ( accessor.SerializeMethod == YamlSerializeMethod.Never )
return; // no need to register
if ( accessor.SerializeMethod == YamlSerializeMethod.Binary ) {
if ( !mType.IsArray )
throw new InvalidOperationException("{0} {1} of {2} is not an array. Can not be serialized as binary."
.DoFormat(mType.FullName, m.Name, type.FullName));
if ( !TypeUtils.IsPureValueType(mType.GetElementType()) )
throw new InvalidOperationException(
"{0} is not a pure ValueType. {1} {2} of {3} can not serialize as binary."
.DoFormat(mType.GetElementType(), mType.FullName, m.Name, type.FullName));
}
// ShouldSerialize
// YamlSerializeAttribute(Never) => false
// ShouldSerializeSomeProperty => call it
// DefaultValueAttribute(default) => compare to it
// otherwise => true
var shouldSerialize = type.GetMethod("ShouldSerialize" + m.Name,
System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic,
null, Type.EmptyTypes, new System.Reflection.ParameterModifier[0]);
if ( shouldSerialize != null && shouldSerialize.ReturnType == typeof(bool) && accessor.ShouldSeriealize == null )
accessor.ShouldSeriealize = obj => (bool)shouldSerialize.Invoke(obj, EmptyObjectArray);
var attr2 = m.GetAttribute<DefaultValueAttribute>();
if ( attr2 != null && accessor.ShouldSeriealize == null ) {
var defaultValue = attr2.Value;
if ( TypeUtils.IsNumeric(defaultValue) && defaultValue.GetType() != mType )
defaultValue = TypeUtils.CastToNumericType(defaultValue, mType);
accessor.ShouldSeriealize = obj => !TypeUtils.AreEqual(defaultValue, accessor.Get(obj));
}
if ( accessor.ShouldSeriealize == null )
accessor.ShouldSeriealize = obj => true;
Accessors.Add(m.Name, accessor);
}