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


C# Type.GetCustomAttributes方法代码示例

本文整理汇总了C#中System.Type.GetCustomAttributes方法的典型用法代码示例。如果您正苦于以下问题:C# Type.GetCustomAttributes方法的具体用法?C# Type.GetCustomAttributes怎么用?C# Type.GetCustomAttributes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Type的用法示例。


在下文中一共展示了Type.GetCustomAttributes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: RecordInfo

        public RecordInfo(Type type)
        {
            if (!typeof(Record).IsAssignableFrom(type))
            {
                throw new ArgumentException("Record type is not derived from Record: " + type.FullName);
            }

            var attribute = type.GetCustomAttributes(typeof(RecordAttribute), false).Cast<RecordAttribute>().FirstOrDefault();// .NET 4.5: type.GetCustomAttribute<RecordAttribute>();
            if (attribute == null)
            {
                throw new ArgumentException("Record type is missing required atribute: " + type.FullName);
            }
            signature = attribute.Signature;

            var va = type.GetCustomAttributes(typeof(VersionAttribute), false).Cast<VersionAttribute>().FirstOrDefault();// .NET 4.5: type.GetCustomAttribute<RecordAttribute>();
            if (va != null)
            {
                version = va.Version;
            }

            var da = type.GetCustomAttributes(typeof(DummyAttribute), false).Cast<DummyAttribute>().FirstOrDefault();
            isDummyRecord = da != null;

            ctor = type.GetConstructor(paramlessTypes);
            compound = InfoProvider.GetCompoundInfo(type);
        }
开发者ID:unforbidable,项目名称:patcher,代码行数:26,代码来源:RecordInfo.cs

示例2: IsEnumType

 private static bool IsEnumType(Type t, string name)
 {
     return t.IsEnum &&
            t.Name.Equals(name) &&
            t.GetCustomAttributes(typeof(DataContractAttribute), false).Length > 0 &&
            t.GetCustomAttributes(typeof(System.CodeDom.Compiler.GeneratedCodeAttribute), false).Length > 0;
 }
开发者ID:gitter-badger,项目名称:XrmUnitTest,代码行数:7,代码来源:LocalCrmDatabaseOrganizationServiceExecute.cs

示例3: GetMetadata

        public FunctionMetadata GetMetadata(Type functionType)
        {
            FunctionMetadata functionMetadata = new FunctionMetadata(functionType);

            functionMetadata.Id = MD5.GetHashString(functionType.FullName);

            var displayNameAttr = functionType.GetCustomAttribute<DisplayNameAttribute>();
            functionMetadata.DisplayName = displayNameAttr != null ? displayNameAttr.DisplayName : null;

            var categoryAttr = functionType.GetCustomAttribute<CategoryAttribute>();
            functionMetadata.Category = categoryAttr != null ? categoryAttr.Category : null;

            var sectionAttr = functionType.GetCustomAttribute<SectionAttribute>();
            functionMetadata.Section = sectionAttr != null ? sectionAttr.Name : null;

            var descriptionAttr = functionType.GetCustomAttribute<DescriptionAttribute>();
            functionMetadata.Description = descriptionAttr != null ? descriptionAttr.Description : null;

            foreach (var signature in functionType.GetCustomAttributes<FunctionSignatureAttribute>())
            {
                functionMetadata.Signatures.Add(GetFunctionSignatureMetadata(signature));
            }

            foreach (var exampleUsage in functionType.GetCustomAttributes<ExampleUsageAttribute>())
            {
                functionMetadata.ExampleUsages.Add(new ExampleUsage(exampleUsage.Expression, exampleUsage.Result)
                {
                    CanMultipleResults = exampleUsage.CanMultipleResults
                });
            }

            return functionMetadata;
        }
开发者ID:shadercoder,项目名称:MathCore,代码行数:33,代码来源:DefaultFunctionMetadataProvider.cs

示例4: ServiceToMeta

        ServiceMeta ServiceToMeta(Type type, int pos)
        {
            var test = type.GetCustomAttributes();
            var serviceAttr = type.GetCustomAttributes()
                .FirstOrDefault(attr => attr.GetType().BaseType?.Name == "ServiceAttribute" || attr.GetType().Name == "ServiceAttribute");
            var divisionalAttr = type.GetCustomAttributes()
                .FirstOrDefault(attr => attr.GetType().Name == "DivisionalAttribute");
            var divisional = true;

            if (serviceAttr == null)
            {
                return null;
            }
            if (divisionalAttr != null)
            {
                divisional = (bool) divisionalAttr.GetType().GetProperty("Divisional").GetValue(divisionalAttr, null);
            }

            var typeName = type.Name;

            var svcMeta = new ServiceMeta()
            {
                Id = (uint)pos+1,
                Name = TypeNameToName(typeName, serviceAttr.GetType()),
                Type = ServiceTypeToEnum(serviceAttr.GetType()),
                Scope = ServiceNameToEnum(serviceAttr),
                Divisional = divisional,
                Multicast = serviceAttr.GetType().Name.Equals("SyncAttribute") && (bool)serviceAttr.GetType().GetProperty("Multicast").GetValue(serviceAttr, null),
            };

            svcMeta.Methods = type.GetMethods().Select((m, p) => MethodToMeta(svcMeta, m, p)).Where(meta => meta != null).ToList();

            return svcMeta;
        }
开发者ID:fingerpasswang,项目名称:Phial.CodeGenerator,代码行数:34,代码来源:ServiceParser.cs

示例5: GetFullNameForModel

 internal static string GetFullNameForModel(Type modelType, string host)
 {
     string ret = null;
     foreach (ModelNamespace mn in modelType.GetCustomAttributes(typeof(ModelNamespace), false))
     {
         if (mn.Host == host)
         {
             ret = mn.Namespace;
             break;
         }
     }
     if (ret == null)
     {
         foreach (ModelNamespace mn in modelType.GetCustomAttributes(typeof(ModelNamespace), false))
         {
             if (mn.Host == "*")
             {
                 ret = mn.Namespace;
                 break;
             }
         }
     }
     ret = (ret == null ? modelType.Namespace : ret);
     return ret+(ret.EndsWith(".") ? "" : ".")+modelType.Name;
 }
开发者ID:marquismark,项目名称:backbone-dotnet,代码行数:25,代码来源:ModelNamespace.cs

示例6: GetAttribute

        /// <summary>
        /// Extracts the attribute from the specified type.
        /// </summary>
        /// <param name="interfaceType">
        /// The interface type.
        /// </param>
        /// <returns>
        /// The <see cref="ComProgIdAttribute"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="interfaceType"/> is <see langword="null"/>.
        /// </exception>
        public static ComProgIdAttribute GetAttribute(Type interfaceType)
        {
            if (null == interfaceType)
            {
                throw new ArgumentNullException("interfaceType");
            }

            Type attributeType = typeof(ComProgIdAttribute);
            object[] attributes = interfaceType.GetCustomAttributes(attributeType, false);

            if (null == attributes || 0 == attributes.Length)
            {
                Type[] interfaces = interfaceType.GetInterfaces();
                for (int i = 0; i < interfaces.Length; i++)
                {
                    interfaceType = interfaces[i];
                    attributes = interfaceType.GetCustomAttributes(attributeType, false);
                    if (null != attributes && 0 != attributes.Length)
                    {
                        break;
                    }
                }
            }

            if (null == attributes || 0 == attributes.Length)
            {
                return null;
            }
            return (ComProgIdAttribute)attributes[0];
        }
开发者ID:Jusonex,项目名称:ShareX,代码行数:42,代码来源:ComProgIdAttribute.cs

示例7: ProxyAction

        protected ProxyAction(ModuleManager manager, Type classType, Type attributeType)
        {
            if (classType == null) throw new ArgumentNullException("classType");
            if (attributeType == null) throw new ArgumentNullException("attributeType");

            _Manager = manager;
            _ClassType = classType;

            object[] attrs;

            // Guid attribute; get it this way, not as Type.GUID, to be sure the attribute is applied
            attrs = _ClassType.GetCustomAttributes(typeof(GuidAttribute), false);
            if (attrs.Length == 0)
                throw new ModuleException(string.Format(null, "Apply the Guid attribute to the '{0}' class.", _ClassType.Name));

            _Id = new Guid(((GuidAttribute)attrs[0]).Value);

            // Module* attribure
            attrs = _ClassType.GetCustomAttributes(attributeType, false);
            if (attrs.Length == 0)
                throw new ModuleException(string.Format(null, "Apply the '{0}' attribute to the '{1}' class.", attributeType.Name, _ClassType.Name));

            _Attribute = (ModuleActionAttribute)attrs[0];

            Initialize();

            if (_Attribute.Resources)
            {
                _Manager.CachedResources = true;
                string name = _Manager.GetString(_Attribute.Name);
                if (!string.IsNullOrEmpty(name))
                    _Attribute.Name = name;
            }
        }
开发者ID:pezipink,项目名称:FarNet,代码行数:34,代码来源:ProxyAction.cs

示例8: RestEndPoint

 // Methods
 private RestEndPoint(Type endPointType, bool isAsync)
 {
     this._endPointType = endPointType;
     this._operations = new Dictionary<string, RestOperation>();
     this._operationList = new List<RestOperation>();
     this._types = new Dictionary<Type, RestType>();
     this._isAsync = isAsync;
     object[] customAttributes = endPointType.GetCustomAttributes(typeof(RestEndPointAttribute), true);
     if ((customAttributes != null) && (customAttributes.Length != 0))
     {
         this._metaInfo = (RestEndPointAttribute)customAttributes[0];
         this._name = this._metaInfo.Name;
     }
     if (string.IsNullOrEmpty(this._name))
     {
         this._name = endPointType.Name;
     }
     customAttributes = endPointType.GetCustomAttributes(typeof(RestDescriptionAttribute), true);
     if ((customAttributes != null) && (customAttributes.Length != 0))
     {
         this._description = ((RestDescriptionAttribute)customAttributes[0]).Description;
     }
     if (this._description == null)
     {
         this._description = string.Empty;
     }
 }
开发者ID:ericklombardo,项目名称:Nuaguil.Net,代码行数:28,代码来源:RestEndPoint.cs

示例9: GetControllerRouteInfo

        private static ControllerRouteInfo GetControllerRouteInfo(Type controllerType)
        {
            string getRoute = null;
            string postRoute = null;
            string putRoute = null;
            string deleteRoute = null;

            var attributes = controllerType.GetCustomAttributes(typeof(GetAttribute), false);

            if (attributes.Length > 0)
                getRoute = ((GetAttribute)attributes[0]).Route;

            attributes = controllerType.GetCustomAttributes(typeof(PostAttribute), false);

            if (attributes.Length > 0)
                postRoute = ((PostAttribute)attributes[0]).Route;

            attributes = controllerType.GetCustomAttributes(typeof(PutAttribute), false);

            if (attributes.Length > 0)
                putRoute = ((PutAttribute)attributes[0]).Route;

            attributes = controllerType.GetCustomAttributes(typeof(DeleteAttribute), false);

            if (attributes.Length > 0)
                deleteRoute = ((DeleteAttribute)attributes[0]).Route;

            return !string.IsNullOrEmpty(getRoute)
                   || !string.IsNullOrEmpty(postRoute)
                   || !string.IsNullOrEmpty(putRoute)
                   || !string.IsNullOrEmpty(deleteRoute)
                ? new ControllerRouteInfo(getRoute, postRoute, putRoute, deleteRoute)
                : null;
        }
开发者ID:jmptrader,项目名称:AcspNet,代码行数:34,代码来源:ControllerMetaDataFactory.cs

示例10: GetNamespace

 /// <summary>
 /// Gets the namespace from an attribute marked on the type's definition
 /// </summary>
 /// <param name="type"></param>
 /// <returns>Namespace of type</returns>
 public static string GetNamespace(Type type)
 {
     Attribute[] attrs = (Attribute[])type.GetCustomAttributes(typeof(DataContractAttribute), true);
     if (attrs.Length > 0)
     {
         DataContractAttribute dcAttr = (DataContractAttribute)attrs[0];
         return dcAttr.Namespace;
     }
     attrs = (Attribute[])type.GetCustomAttributes(typeof(XmlRootAttribute), true);
     if (attrs.Length > 0)
     {
         XmlRootAttribute xmlAttr = (XmlRootAttribute)attrs[0];
         return xmlAttr.Namespace;
     }
     attrs = (Attribute[])type.GetCustomAttributes(typeof(XmlTypeAttribute), true);
     if (attrs.Length > 0)
     {
         XmlTypeAttribute xmlAttr = (XmlTypeAttribute)attrs[0];
         return xmlAttr.Namespace;
     }
     attrs = (Attribute[])type.GetCustomAttributes(typeof(XmlElementAttribute), true);
     if (attrs.Length > 0)
     {
         XmlElementAttribute xmlAttr = (XmlElementAttribute)attrs[0];
         return xmlAttr.Namespace;
     }
     return null;
 }
开发者ID:EvgeniyProtas,项目名称:servicestack,代码行数:33,代码来源:XmlSerializableWrapper.cs

示例11: PluginInformation

		internal PluginInformation(Type type)
		{
			var displayNameAttributes = type.GetCustomAttributes(typeof(DisplayNameAttribute), false) as DisplayNameAttribute[];
			var descriptionAttributes = type.GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[];

			Type = type;
			DisplayName = string.Intern(displayNameAttributes.Length > 0 ? displayNameAttributes[0].DisplayName : type.Name);
			Description = descriptionAttributes.Length > 0 ? descriptionAttributes[0].Description : null;
		}
开发者ID:cros107,项目名称:CrystalBoy,代码行数:9,代码来源:PluginInformation.cs

示例12: GetDataSourceInfo

        /// <summary>
        /// Retrieves data source information.
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        internal DataSourceInfo GetDataSourceInfo(Type type)
        {
            DataSourceInfo info;

            if (type == null)
                throw new ArgumentNullException("type");

            try
            {
                CacheManager mappingCache = CacheFactory.GetCacheManager();

                if (mappingCache.Contains(string.Format("GetDataSourceInfo({0})", type.FullName)))
                    return (DataSourceInfo)mappingCache[string.Format("GetDataSourceInfo({0})", type.FullName)];

                Debug.WriteLine(string.Format("Getting data source information for type {0}", type.Name));

                info = new DataSourceInfo();

                DataSourceAttribute[] datasource = (DataSourceAttribute[])type.GetCustomAttributes(typeof(DataSourceAttribute), true);

                info.DataSourceName = datasource.Length > 0 ? datasource[0].Name : string.Empty;

                // Retrieving table name to map the object in the database.
                TableAttribute[] tables = (TableAttribute[])type.GetCustomAttributes(typeof(TableAttribute), true);

                info.DeleteCommandName = datasource.Length > 0 &&
                    !string.IsNullOrEmpty(datasource[0].DeleteCommandName) ?
                    datasource[0].DeleteCommandName : "Delete" + (tables.Length > 0 &&
                    !string.IsNullOrEmpty(tables[0].Name) ? tables[0].Name : type.Name);
                info.InsertCommandName = datasource.Length > 0 &&
                    !string.IsNullOrEmpty(datasource[0].InsertCommandName) ?
                    datasource[0].InsertCommandName : "Insert" + (tables.Length > 0 &&
                    !string.IsNullOrEmpty(tables[0].Name) ? tables[0].Name : type.Name);
                info.UpdateCommandName = datasource.Length > 0 &&
                    !string.IsNullOrEmpty(datasource[0].UpdateCommandName) ?
                    datasource[0].UpdateCommandName : "Update" + (tables.Length > 0 &&
                    !string.IsNullOrEmpty(tables[0].Name) ? tables[0].Name : type.Name);
                info.SelectCommandName = datasource.Length > 0 &&
                    !string.IsNullOrEmpty(datasource[0].SelectCommandName) ?
                    datasource[0].SelectCommandName : "Select" + (tables.Length > 0 &&
                    !string.IsNullOrEmpty(tables[0].Name) ? tables[0].Name : type.Name);
                info.DataSetAdapterName = datasource.Length > 0 &&
                    !string.IsNullOrEmpty(datasource[0].DataSetAdapterName) ?
                    datasource[0].DataSetAdapterName : (tables.Length > 0 &&
                    !string.IsNullOrEmpty(tables[0].Name) ? tables[0].Name : type.Name) + "Adapter";

                mappingCache.Add(string.Format("GetDataSourceInfo({0})", type.FullName), info,
                    CacheItemPriority.None, null, new SlidingTime(TimeSpan.FromMinutes(15)));
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return info;
        }
开发者ID:kohku,项目名称:codefactory,代码行数:61,代码来源:DataObjectManager.cs

示例13: ConfigDataTypeAttribute

        public ConfigDataTypeAttribute( Type configDataType )
        {
            if ( configDataType == null )
            {
                throw new ArgumentNullException( "configDataType", "The given config data type must not be null." );
            }

            bool isSerializable = false;
            bool isXmlRoot = false;
            bool overridesEquals = false;
            //bool isEquatable = false;

            // check if overrides object.Equals()
            foreach ( var info in configDataType.GetMethods() )
            {
                if ( info.Name.Equals( "Equals" ) && info.DeclaringType.Equals( configDataType ) && info.GetBaseDefinition().DeclaringType.Equals( typeof( object ) ) )
                {
                    overridesEquals = true;
                    break;
                }
            }

            // check if serializable
            if ( configDataType.GetCustomAttributes( typeof( SerializableAttribute ), false ).Length > 0 )
            {
                isSerializable = true;
            }

            // check if xmlroot
            if ( configDataType.GetCustomAttributes( typeof( System.Xml.Serialization.XmlRootAttribute ), false ).Length > 0 )
            {
                isXmlRoot = true;
            }

            // make sure config data type overrides object.Equals()
            if ( !overridesEquals )
            {
                throw new ArgumentException( "The given configuratioun data type '" + configDataType.FullName + "' must override 'object.Equals(object obj)'.", "configDataType" );
            }

            // make sure config data type is serializable
            if ( !isSerializable )
            {
                throw new ArgumentException( "The given configuratioun data type '" + configDataType.FullName + "' must be decorated with the 'System.SerializableAttribute'.", "configDataType" );
            }

            // make sure the config data type is an xml root
            if ( !isXmlRoot )
            {
                throw new ArgumentException( "The given configuration data type '" + configDataType.FullName + "' must be decorated with the 'System.Xml.Serialization.XmlRootAttribute'.", "configDataType" );
            }

            // store data type
            this.ConfigDataType = configDataType;
        }
开发者ID:galaktor,项目名称:ngin,代码行数:55,代码来源:ConfigDataTypeAttribute.cs

示例14: _AppendAttributes

 private void _AppendAttributes(Type modelType, WrappedStringBuilder sb,bool minimize)
 {
     if (modelType.GetCustomAttributes(typeof(ModelViewAttribute), false).Length > 0)
     {
         sb.Append((minimize ? "attributes:{":"\tattributes: {"));
         object[] atts = modelType.GetCustomAttributes(typeof(ModelViewAttribute),false);
         for (int x = 0; x < atts.Length; x++)
             sb.Append((minimize ? "" : "\t\t")+"\"" + ((ModelViewAttribute)atts[x]).Name + "\" : '" + ((ModelViewAttribute)atts[x]).Value + "'" + (x < atts.Length - 1 ? "," : ""));
         sb.Append((minimize ? "" : "\t")+"},");
     }
 }
开发者ID:marquismark,项目名称:backbone-dotnet,代码行数:11,代码来源:ViewGenerator.cs

示例15: LoadAttributesAsDomainModel

        private TaskDefinition LoadAttributesAsDomainModel(Type type)
        {
            object[] taskAttributes = type.GetCustomAttributes(typeof(TaskAttribute), true);
            object[] settingAttributes = type.GetCustomAttributes(typeof(TaskSettingAttribute), true);

            bool typeHasAttributes = (taskAttributes.Count() > 0 && taskAttributes is TaskAttribute[]);
            if (!typeHasAttributes)
                return null;

            return ToDomainModel(((TaskAttribute[])taskAttributes)[0], settingAttributes);
        }
开发者ID:ArildF,项目名称:Smeedee,代码行数:11,代码来源:TaskDefinitionLoader.cs


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