本文整理汇总了C#中System.Reflection.PropertyInfo类的典型用法代码示例。如果您正苦于以下问题:C# PropertyInfo类的具体用法?C# PropertyInfo怎么用?C# PropertyInfo使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
PropertyInfo类属于System.Reflection命名空间,在下文中一共展示了PropertyInfo类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetValueFromXml
protected override object GetValueFromXml(XElement root, XName name, PropertyInfo prop)
{
var isAttribute = false;
// Check for the DeserializeAs attribute on the property
var options = prop.GetAttribute<DeserializeAsAttribute>();
if (options != null)
{
name = options.Name ?? name;
isAttribute = options.Attribute;
}
if (isAttribute)
{
var attributeVal = GetAttributeByName(root, name);
if (attributeVal != null)
{
return attributeVal.Value;
}
}
return base.GetValueFromXml(root, name, prop);
}
示例2: PropertyToElementInit
static ElementInit PropertyToElementInit(PropertyInfo propertyInfo, Expression instance)
{
return Expression.ElementInit(DictionaryAddMethod,
Expression.Constant(propertyInfo.Name),
Expression.Call(ToStringOrNullMethod,
Expression.Convert(Expression.Property(instance, propertyInfo), typeof(object))));
}
示例3: Create
public static ConstantUpdate Create(PropertyInfo property, ConstantExpression constantExpr)
{
Debug.Assert(property != null, "property should not be null");
Debug.Assert(constantExpr != null, "constantExpr should not be null");
return new ConstantUpdate(property, constantExpr.Value);
}
示例4: MvxBasePropertyInfoSourceBinding
protected MvxBasePropertyInfoSourceBinding(object source, string propertyName)
: base(source)
{
_propertyName = propertyName;
if (Source == null)
{
MvxBindingTrace.Trace(
MvxTraceLevel.Warning,
"Unable to bind to source is null"
, propertyName);
return;
}
_propertyInfo = source.GetType().GetProperty(propertyName);
if (_propertyInfo == null)
{
MvxBindingTrace.Trace(
MvxTraceLevel.Warning,
"Unable to bind: source property source not found {0} on {1}"
, propertyName,
source.GetType().Name);
}
var sourceNotify = Source as INotifyPropertyChanged;
if (sourceNotify != null)
sourceNotify.PropertyChanged += new PropertyChangedEventHandler(SourcePropertyChanged);
}
示例5: CreateRelationObject
/// <summary>
/// Creates one instance of the Relation Object from the PropertyInfo object of the reflected class
/// </summary>
/// <param name="propInfo"></param>
/// <returns></returns>
public static Relation CreateRelationObject(PropertyInfo propInfo)
{
Relation result = new VSPlugin.Relation();
// get's the Relation Attribute in the class
RelationAttribute relationAttribute = propInfo.GetCustomAttribute<RelationAttribute>();
if (relationAttribute== null)
return result;
Type elementType = null;
//we need to discover the id of the relation and if it's a collection
if (propInfo.PropertyType.IsArray)
{
result.Collection = true;
elementType = propInfo.PropertyType.GetElementType();
}
else if(propInfo.PropertyType.IsGenericType && propInfo.PropertyType.GetGenericTypeDefinition() == typeof(List<>))
{
result.Collection = true;
elementType = propInfo.PropertyType.GetGenericArguments().Single();
}
else
elementType = propInfo.PropertyType;
result.Type = GetResourceType(elementType);
result.Required = relationAttribute.Required;
result.Requirement = relationAttribute.Requirement;
result.Allocate = relationAttribute.Allocate.ToString();
return result;
}
示例6: CreateGetMethod
internal static GenericGetter CreateGetMethod(Type type, PropertyInfo propertyInfo)
{
MethodInfo getMethod = propertyInfo.GetGetMethod();
if (getMethod == null)
return null;
DynamicMethod getter = new DynamicMethod("_", typeof(object), new Type[] { typeof(object) }, type);
ILGenerator il = getter.GetILGenerator();
if (!type.IsClass) // structs
{
var lv = il.DeclareLocal(type);
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Unbox_Any, type);
il.Emit(OpCodes.Stloc_0);
il.Emit(OpCodes.Ldloca_S, lv);
il.EmitCall(OpCodes.Call, getMethod, null);
if (propertyInfo.PropertyType.IsValueType)
il.Emit(OpCodes.Box, propertyInfo.PropertyType);
}
else
{
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Castclass, propertyInfo.DeclaringType);
il.EmitCall(OpCodes.Callvirt, getMethod, null);
if (propertyInfo.PropertyType.IsValueType)
il.Emit(OpCodes.Box, propertyInfo.PropertyType);
}
il.Emit(OpCodes.Ret);
return (GenericGetter)getter.CreateDelegate(typeof(GenericGetter));
}
示例7: GetBackingFieldInternal
//Supports getters of the form `return field;`
private static FieldInfo GetBackingFieldInternal(PropertyInfo property)
{
if (property == null)
throw new ArgumentNullException("property");
MethodInfo getter = property.GetGetMethod(true);
if (getter == null)
return null;
byte[] il = getter.GetMethodBody().GetILAsByteArray();
if (il.Length != 7
|| il[0] != 0x02//ldarg.0
|| il[1] != 0x7b//ldfld <field>
|| il[6] != 0x2a//ret
)
return null;
int metadataToken = il[2] | il[3] << 8 | il[4] << 16 | il[5] << 24;
Type type = property.ReflectedType;
do
{
foreach (FieldInfo fi in type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
{
if (fi.MetadataToken == metadataToken)
return fi;
}
type = type.BaseType;
} while (type != null);
throw new Exception("Field not found");
}
示例8: ProcessProperty
/// <summary>
/// Dispatches the call to the extensions.
/// </summary>
/// <param name="pi">The property info reflection object.</param>
/// <param name="model">The model.</param>
public void ProcessProperty(PropertyInfo pi, ActiveRecordModel model)
{
foreach(IModelBuilderExtension extension in extensions)
{
extension.ProcessProperty(pi, model);
}
}
示例9: GetLocalizationInfo
private static LocalizationInfo GetLocalizationInfo(PropertyInfo prop)
{
var attr = prop.GetCustomAttribute<LocalizeAttribute>();
if(attr == null)
{
return null;
}
var info = new LocalizationInfo
{
ResourceName = attr.ResourceName ?? (prop.DeclaringType.Name + "_" + prop.Name)
};
if(info.ResourceType != null)
{
info.ResourceType = attr.ResourceType;
}
else
{
var parent = prop.DeclaringType.GetTypeInfo().GetCustomAttribute<LocalizeAttribute>();
if(parent == null || parent.ResourceType == null)
{
throw new ArgumentException(String.Format(
"The property '{0}' or its parent class '{1}' must define a ResourceType in order to be localized.",
prop.Name, prop.DeclaringType.Name));
}
info.ResourceType = parent.ResourceType;
}
return info;
}
示例10: MatchTest
public MatchResult MatchTest(PropertyInfo property)
{
if (property.PropertyType.Equals(typeof(DateTime)))
return MatchResult.Match;
else
return MatchResult.NotMatch;
}
示例11: IsValid
public ValidationResult IsValid(PropertyInfo propertyToValidate, object value)
{
// enable to be empty
if (value == null)
return ValidationResult.Success;
if (!(value is DateTime))
{
return ValidationResult.CreateError(propertyToValidate, "Adf.Business.AttributeInRangeInvalid", propertyToValidate.Name);
}
// double newValue;
// try
// {
// newValue = (double)value;
// }
// catch
// {
// return ValidationResult.CreateError(propertyToValidate, "Adf.Business.AttributeInRangeInvalid", propertyToValidate.Name);
// }
DateTime newValue = (DateTime) value;
if (min >= newValue || max <= newValue)
{
return ValidationResult.CreateError(propertyToValidate, "Adf.Business.AttributeInRangeInvalidRange", propertyToValidate.Name, min, max);
}
// if (!NumberHelper.CheckRange(newValue, min, max))
// return ValidationResult.CreateError(propertyToValidate, "Adf.Business.AttributeInRangeInvalidRange", propertyToValidate.Name, min, max);
return ValidationResult.Success;
}
示例12: ProcessBelongsTo
/// <summary>
/// Dispatches the call to the extensions.
/// </summary>
/// <param name="pi">The property info reflection object.</param>
/// <param name="belongsToModel">The belongs to model.</param>
/// <param name="model">The model.</param>
public void ProcessBelongsTo(PropertyInfo pi, BelongsToModel belongsToModel, ActiveRecordModel model)
{
foreach(IModelBuilderExtension extension in extensions)
{
extension.ProcessBelongsTo(pi, belongsToModel, model);
}
}
示例13: DynamicPropertyAccessor
public DynamicPropertyAccessor(PropertyInfo propertyInfo)
{
// target: (object)((({TargetType})instance).{Property})
// preparing parameter, object type
ParameterExpression instance = Expression.Parameter(
typeof(object), "instance");
// ({TargetType})instance
Expression instanceCast = Expression.Convert(
instance, propertyInfo.ReflectedType);
// (({TargetType})instance).{Property}
Expression propertyAccess = Expression.Property(
instanceCast, propertyInfo);
// (object)((({TargetType})instance).{Property})
UnaryExpression castPropertyValue = Expression.Convert(
propertyAccess, typeof(object));
// Lambda expression
Expression<Func<object, object>> lambda =
Expression.Lambda<Func<object, object>>(
castPropertyValue, instance);
this.m_getter = lambda.Compile();
MethodInfo setMethod = propertyInfo.GetSetMethod();
if (setMethod != null) {
this.m_dynamicSetter = new DynamicMethodExecutor(setMethod);
}
}
示例14: GetValueOnField
public virtual object GetValueOnField(object valueOnDomain, object value, PropertyInfo propertyInfo)
{
object valueOnField = value;
if (valueOnDomain != null && typeof (CustomEnum).IsAssignableFrom(propertyInfo.PropertyType))
valueOnField = CustomEnum.ValueOf(propertyInfo.PropertyType, value.ToString());
return valueOnField;
}
示例15: GetPropertyInterceptors
protected IList<IPropertyInterceptor> GetPropertyInterceptors(PropertyInfo Property)
{
if (this.PropertyInterceptorRegistrations.ContainsKey(Property))
return this.PropertyInterceptorRegistrations[Property];
else
return null;
}