本文整理汇总了C#中System.Reflection.MemberInfo.AllAttributes方法的典型用法代码示例。如果您正苦于以下问题:C# MemberInfo.AllAttributes方法的具体用法?C# MemberInfo.AllAttributes怎么用?C# MemberInfo.AllAttributes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.MemberInfo
的用法示例。
在下文中一共展示了MemberInfo.AllAttributes方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateForMember
static InjectableInfo CreateForMember(MemberInfo memInfo, Type parentType)
{
var identifier = memInfo.AllAttributes<InjectAttribute>().Select(x => x.Identifier)
.Concat(memInfo.AllAttributes<InjectOptionalAttribute>().Select(x => x.Identifier)).FirstOrDefault();
Type memberType;
Action<object, object> setter;
if (memInfo is FieldInfo)
{
var fieldInfo = (FieldInfo)memInfo;
setter = ((object injectable, object value) => fieldInfo.SetValue(injectable, value));
memberType = fieldInfo.FieldType;
}
else
{
Assert.That(memInfo is PropertyInfo);
var propInfo = (PropertyInfo)memInfo;
setter = ((object injectable, object value) => propInfo.SetValue(injectable, value, null));
memberType = propInfo.PropertyType;
}
return new InjectableInfo(
memInfo.HasAttribute(typeof(InjectOptionalAttribute)),
identifier,
memInfo.Name,
memberType,
parentType,
setter);
}
示例2: CreateForMember
static InjectableInfo CreateForMember(MemberInfo memInfo, Type parentType)
{
var injectAttributes = memInfo.AllAttributes<InjectAttribute>().ToList();
var injectOptionalAttributes = memInfo.AllAttributes<InjectOptionalAttribute>().ToList();
string identifier = null;
Assert.That(injectAttributes.IsEmpty() || injectOptionalAttributes.IsEmpty(),
"Found both 'InjectOptional' and 'Inject' attributes on type field '{0}' of type '{1}'. Field should only have one or the other.", memInfo.Name, parentType.Name());
if (injectAttributes.Any())
{
identifier = injectAttributes.Single().Identifier;
}
else if (injectOptionalAttributes.Any())
{
identifier = injectOptionalAttributes.Single().Identifier;
}
Type memberType;
Action<object, object> setter;
if (memInfo is FieldInfo)
{
var fieldInfo = (FieldInfo)memInfo;
setter = ((object injectable, object value) => fieldInfo.SetValue(injectable, value));
memberType = fieldInfo.FieldType;
}
else
{
Assert.That(memInfo is PropertyInfo);
var propInfo = (PropertyInfo)memInfo;
setter = ((object injectable, object value) => propInfo.SetValue(injectable, value, null));
memberType = propInfo.PropertyType;
}
return new InjectableInfo(
injectOptionalAttributes.Any(),
identifier,
memInfo.Name,
memberType,
parentType,
setter,
null);
}
示例3: CreateForMember
static InjectableInfo CreateForMember(MemberInfo memInfo, Type parentType)
{
var injectAttributes = memInfo.AllAttributes<InjectAttributeBase>().ToList();
Assert.That(injectAttributes.Count <= 1,
"Found multiple 'Inject' attributes on type field '{0}' of type '{1}'. Field should only container one Inject attribute", memInfo.Name, parentType.Name());
var injectAttr = injectAttributes.SingleOrDefault();
string identifier = null;
bool isOptional = false;
bool localOnly = false;
if (injectAttr != null)
{
identifier = injectAttr.Identifier;
isOptional = injectAttr.IsOptional;
localOnly = injectAttr.LocalOnly;
}
Type memberType;
Action<object, object> setter;
if (memInfo is FieldInfo)
{
var fieldInfo = (FieldInfo)memInfo;
setter = ((object injectable, object value) => fieldInfo.SetValue(injectable, value));
memberType = fieldInfo.FieldType;
}
else
{
Assert.That(memInfo is PropertyInfo);
var propInfo = (PropertyInfo)memInfo;
setter = ((object injectable, object value) => propInfo.SetValue(injectable, value, null));
memberType = propInfo.PropertyType;
}
return new InjectableInfo(
isOptional,
identifier,
memInfo.Name,
memberType,
parentType,
setter,
null,
localOnly);
}