当前位置: 首页>>代码示例>>C#>>正文


C# MemberInfo.AllAttributes方法代码示例

本文整理汇总了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);
        }
开发者ID:IllusiveS,项目名称:Asteroids,代码行数:30,代码来源:TypeAnalyzer.cs

示例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);
        }
开发者ID:JonJam,项目名称:marveluniverse,代码行数:45,代码来源:TypeAnalyzer.cs

示例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);
        }
开发者ID:4xisblack,项目名称:Zenject,代码行数:47,代码来源:TypeAnalyzer.cs


注:本文中的System.Reflection.MemberInfo.AllAttributes方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。