本文整理汇总了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);
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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];
}
示例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;
}
}
示例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;
}
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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")+"},");
}
}
示例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);
}