private static void RegisterConVars(Assembly assembly) { List <BaseConVar> customVars = new List <BaseConVar>(); foreach (Type type in assembly.GetTypes()) { foreach (FieldInfo field in type.GetFields(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)) { if (field.FieldType.IsSubclassOf(typeof(BaseConVar))) { if (field.IsStatic) { console.InvokeMethod("RegisterConVarInternal", (BaseConVar)field.GetValue(null)); //console.RegisterConVarInternal((BaseConVar)field.GetValue(null));//This line fails on Release. customVars.Add((BaseConVar)field.GetValue(null)); } else if (CustomAttributeExtensions.GetCustomAttribute <CompilerGeneratedAttribute>(type) == null) { Debug.LogErrorFormat("ConVar defined as {0} in {1}. {2} could not be registered. ConVars must be static fields.", type.Name, assembly.FullName, field.Name); } } } } foreach (BaseConVar baseConVar in customVars) { if ((baseConVar.flags & ConVarFlags.Engine) != ConVarFlags.None) { baseConVar.defaultValue = baseConVar.GetString(); } else if (baseConVar.defaultValue != null) { baseConVar.SetString(baseConVar.defaultValue); } } }
public static string GetDescription(this Enum enumVal) { MemberInfo[] memInfo = enumVal.GetType().GetMember(enumVal.ToString()); DescriptionAttribute attribute = CustomAttributeExtensions.GetCustomAttribute <DescriptionAttribute>(memInfo[0]); return(attribute.Description); }
/// <summary> /// 轉成 DataTable, T 的欄位順序必須要與 user defined table type 的欄位順序一致 ! /// </summary> public static DataTable ToDataTable <T>(this IEnumerable <T> objs) { var dt = new DataTable(); var propertyInfos = typeof(T).GetProperties() .Where(p => CustomAttributeExtensions.GetCustomAttribute <IgnoreDataTableAttribute>(p) == null); foreach (var p in propertyInfos) { var dc = new DataColumn(p.Name, Nullable.GetUnderlyingType(p.PropertyType) ?? p.PropertyType); dc.AllowDBNull = true; dt.Columns.Add(dc); } foreach (T entity in objs) { DataRow dr = dt.NewRow(); foreach (PropertyInfo p in propertyInfos) { var value = p.GetValue(entity); dr[p.Name] = value ?? DBNull.Value; } dt.Rows.Add(dr); } return(dt); }
public static string ToTZinfo(this TimeZoneFromTwitter language) { var field = language.GetType().GetField(language.ToString()); var descriptionAttribute = (TimeZoneFromTwitterAttribute)CustomAttributeExtensions.GetCustomAttribute(field, typeof(TimeZoneFromTwitterAttribute)); return(descriptionAttribute != null ? descriptionAttribute.TZinfo : language.ToString()); }
public static string GetBuildNumber() { var assembly = typeof(AssemblyBuildNumberAttribute).GetTypeInfo().Assembly; var attribute = CustomAttributeExtensions.GetCustomAttribute <AssemblyBuildNumberAttribute>(assembly); return(attribute.BuildNumber); }
public static IEnumerable <Tuple <MemberInfo, TAttribute> > FindMemberAttributes <TAttribute>( this Type type, bool nonPublic, BindingFlags bindingflags) where TAttribute : Attribute { if (type == null) { throw new ArgumentNullException(nameof(type)); } bindingflags |= BindingFlags.Public; if (nonPublic) { bindingflags |= BindingFlags.NonPublic; } if (!Enum.IsDefined(typeof(BindingFlags), BindingFlags.FlattenHierarchy)) { return((from mem in type.GetMembers(bindingflags) let attr = CustomAttributeExtensions.GetCustomAttribute <TAttribute>(mem) where attr != null select Tuple.Create(mem, attr)) !); } return((from mem in type.GetHieratichialMembers(bindingflags) let attr = mem.GetCustomAttribute <TAttribute>() where attr.HasValue select Tuple.Create(mem, attr.Value)) !); }
private IEnumerable <Tuple <FieldInfo, PacketFieldAttribute> > GetFieldsOnPacket(Type type) { return(type.GetFields() .Select(f => Tuple.Create <FieldInfo, PacketFieldAttribute>(f, CustomAttributeExtensions.GetCustomAttribute <PacketFieldAttribute>((MemberInfo)f))) .Where(t => t.Item2 != null) .OrderBy(t => t.Item2.Order)); }
public static string GetLanguageCode(this LanguageFilter?language) { var field = language.GetType().GetField(language.ToString()); var descriptionAttribute = (LanguageAttribute)CustomAttributeExtensions.GetCustomAttribute(field, typeof(LanguageAttribute)); return(descriptionAttribute != null ? descriptionAttribute.Code : language.ToString()); }
private string GetConnectionNameFromType() { Type entitytype = typeof(T); string collectionname; var att = CustomAttributeExtensions.GetCustomAttribute <ConnectionNameAttribute>(typeof(T).GetTypeInfo().Assembly); if (att != null) { collectionname = att.Name; } else { if (typeof(Entity).GetTypeInfo().IsAssignableFrom(entitytype)) { while (!entitytype.GetTypeInfo().BaseType.Equals(typeof(Entity))) { entitytype = entitytype.GetTypeInfo().BaseType; } } collectionname = entitytype.Name; } return(collectionname); }
private static string GetEnumDescription(Enum enumVal) { System.Reflection.MemberInfo[] memInfo = enumVal.GetType().GetMember(enumVal.ToString()); DescriptionAttribute attribute = CustomAttributeExtensions.GetCustomAttribute <DescriptionAttribute>(memInfo[0]); return(attribute.Description); }
private string ConvertToString(object value, CultureInfo cultureInfo) { if (value is Enum) { var name = Enum.GetName(value.GetType(), value); if (name != null) { var field = IntrospectionExtensions.GetTypeInfo(value.GetType()).GetDeclaredField(name); if (field != null) { var attribute = CustomAttributeExtensions.GetCustomAttribute(field, typeof(EnumMemberAttribute)) as EnumMemberAttribute; if (attribute != null) { return(attribute.Value != null ? attribute.Value : name); } } } } else if (value is bool) { return(Convert.ToString(value, cultureInfo).ToLowerInvariant()); } else if (value is byte[]) { return(Convert.ToBase64String((byte[])value)); } else if (value != null && value.GetType().IsArray) { var array = Enumerable.OfType <object>((Array)value); return(string.Join(",", Enumerable.Select(array, o => ConvertToString(o, cultureInfo)))); } return(Convert.ToString(value, cultureInfo)); }
public virtual void Apply(AbstractNode abstractNode, SemanticDocument doc) { var type = abstractNode.GetType().GetTypeInfo(); var markedUpProps = type.DeclaredProperties .Select(propertyInfo => new { propertyInfo = propertyInfo, att = CustomAttributeExtensions.GetCustomAttribute <ExposeAttribute>((MemberInfo)propertyInfo), propertyUri = UriHelper.Combine(abstractNode.Uri, propertyInfo.Name) }).Where(x => x.att != null); foreach (var x in markedUpProps) { if (typeof(AbstractNode).GetTypeInfo().IsAssignableFrom(x.propertyInfo.PropertyType.GetTypeInfo())) { var childNode = (AbstractNode)x.propertyInfo.GetValue(abstractNode); doc.AddLink(Links.CreateLink(childNode.Title, childNode.Uri, childNode.Term)); } else { doc.Value.Add(SemanticProperty.CreateValue(TermFactory.From(x.propertyInfo), JToken.FromObject(x.propertyInfo.GetValue(abstractNode)))); } } }
/// <summary> /// Determines the connection name from the specified type. /// </summary> /// <param name="entitytype">The type of the entity to get the connection name from.</param> /// <returns>Returns the connection name from the specified type.</returns> private static string GetConnectionNameFromType() { Type entitytype = typeof(T); string collectionname; // Check to see if the object (inherited from Entity) has a ConnectionName attribute var att = CustomAttributeExtensions.GetCustomAttribute <ConnectionNameAttribute>(typeof(T).GetTypeInfo().Assembly); if (att != null) { // It does! Return the value specified by the ConnectionName attribute collectionname = att.Name; } else { if (typeof(Entity).GetTypeInfo().IsAssignableFrom(entitytype)) { // No attribute found, get the basetype while (!entitytype.GetTypeInfo().BaseType.Equals(typeof(Entity))) { entitytype = entitytype.GetTypeInfo().BaseType; } } collectionname = entitytype.Name; } return(collectionname); }
private static Tuple <string, string> GetAssemblyVersions(Assembly asm, bool usePackageVersion) { AssemblyName asmName = asm.GetName(); string name = asmName.Name.ToString(); string version = null; if (usePackageVersion) { // usually a string matching the AssemblyFileVersion plus the prerelease suffix if packaged with NuGet var infoAttr = CustomAttributeExtensions.GetCustomAttribute <AssemblyInformationalVersionAttribute>(asm); if (infoAttr != null) { version = infoAttr.InformationalVersion; } } if (version == null) { var attr = CustomAttributeExtensions.GetCustomAttribute <AssemblyFileVersionAttribute>(asm); if (attr != null) { version = attr.Version; } } if (version == null) { version = asmName.Version.ToString(); // Microsoft typically only sets the major version } return(new Tuple <string, string>(name, version)); }
private void AddTrait(Type trait) { var path = new Queue <string>(); var loc = CustomAttributeExtensions.GetCustomAttribute <TraitLocationAttribute>(trait); if (loc != null) { foreach (var s in loc.Path.Split('/')) { path.Enqueue(s); } } var toAddOn = entity.gameObject.transform; foreach (var s in path) { var found = toAddOn.transform.Find(s); if (found == null) { found = new GameObject(s).transform; found.SetParent(toAddOn); } toAddOn = found; } toAddOn.gameObject.AddComponent(trait); }
/// <summary> /// Determines the connection name from the specified type. /// </summary> /// <typeparam name="T">The type to get the connection name from.</typeparam> /// <returns>Returns the connection name from the specified type.</returns> private static string GetConnectionNameFromInterface() { // Check to see if the object (inherited from Entity) has a ConnectionName attribute var att = CustomAttributeExtensions.GetCustomAttribute <ConnectionNameAttribute>(typeof(T).GetTypeInfo().Assembly); return(att?.Name ?? typeof(T).Name); }
private string CreateKey() { var primitiveTypeFields = from field in typeof(Primitive).GetFields(BindingFlags.Static) let attribute = CustomAttributeExtensions.GetCustomAttribute <RuntimeKeyAttribute>(field) where attribute != null select new { Field = field, Attribute = attribute }; var primitiveDictionary = primitiveTypeFields.ToDictionary(x => (Primitive)x.Field.GetValue(null), x => x.Attribute.Key); var currentKey = primitiveDictionary[Primitive]; if (Affinity.Size.x > 1) { return($"{currentKey}{Affinity.Size.x}x{Affinity.Size.y}"); } if (Affinity.Size.y > 1) { return($"{currentKey}{Affinity.Size.y}"); } return(currentKey); }
public static string GetCodeVersion() { var assembly = typeof(AssemblyCodeVersionAttribute).GetTypeInfo().Assembly; var attribute = CustomAttributeExtensions.GetCustomAttribute <AssemblyCodeVersionAttribute>(assembly); return(attribute.CodeVersion); }
public static string GetQueryFilterName(this TweetSearchFilters tweetSearchFilters) { var field = tweetSearchFilters.GetType().GetField(tweetSearchFilters.ToString()); var descriptionAttribute = (TweetSearchFilterAttribute)CustomAttributeExtensions.GetCustomAttribute(field, typeof(TweetSearchFilterAttribute)); return(descriptionAttribute != null ? descriptionAttribute.FilterName : tweetSearchFilters.ToString().ToLowerInvariant()); }
public void GetCustomAttribute_Single() { Type type = typeof(TestClass_P); MethodInfo miWithAttributes = type.GetTypeInfo().GetDeclaredMethod("methodWithAttribute"); ParameterInfo piWithAttributes = miWithAttributes.GetParameters()[0]; Attribute attribute = CustomAttributeExtensions.GetCustomAttribute(piWithAttributes, typeof(MyAttribute_Single_P)); Assert.NotNull(attribute); Assert.Throws <AmbiguousMatchException>(() => { attribute = CustomAttributeExtensions.GetCustomAttribute(piWithAttributes, typeof(MyAttribute_AllowMultiple_P)); }); AssertExtensions.Throws <ArgumentException>(null, () => { attribute = CustomAttributeExtensions.GetCustomAttribute(piWithAttributes, typeof(string)); }); Assert.Throws <ArgumentNullException>(() => { attribute = CustomAttributeExtensions.GetCustomAttribute(piWithAttributes, null); }); }
public void GetCustom_Attribute_On_Return_Parameter_On_Parent_Method() { Type type = typeof(TestClass_P_Derived); MethodInfo miWithReturnAttribute = type.GetTypeInfo().GetDeclaredMethod("methodWithReturnAttribute"); ParameterInfo returnParameter = miWithReturnAttribute.ReturnParameter; MyAttribute_Single_P attribute = CustomAttributeExtensions.GetCustomAttribute <MyAttribute_Single_P>(returnParameter, inherit: true); Assert.NotNull(attribute); }
public static DefinitionSet Load(Assembly assembly, Func <Type, bool> typeFilter = null) { var types = assembly.GetTypes() .Where(t => !t.IsNested && CustomAttributeExtensions.GetCustomAttribute <SnapshotDefinitionAttribute>((MemberInfo)t) != null) .Where(t => typeFilter == null || typeFilter(t)) .ToList(); return(LoadDefinitions(types)); }
/// <summary> /// Retrieves a custom attribute that is applied to a specified element. /// </summary> /// <typeparam name="TAttribute">The type of attribute to search for.</typeparam> /// <param name="property">The type to search in.</param> /// <param name="inherit"><c>true</c> to inspect the ancestors of element; otherwise, <c>false</c>.</param> /// <returns> /// A custom attribute that matches <typeparamref name="TAttribute" />, or <c>null</c> if no such attribute is found. /// </returns> public static TAttribute GetCustomAttribute <TAttribute>(this PropertyInfo property, bool inherit = false) where TAttribute : Attribute { #if NET40 var attributes = property.GetCustomAttributes(typeof(TAttribute), inherit); return(0 < attributes.Length ? (TAttribute)attributes[0] : null); #else return(CustomAttributeExtensions.GetCustomAttribute <TAttribute>(property, inherit)); #endif }
private static TimeZoneFromTwitterAttribute GetAttribute(TimeZoneFromTwitter timeZoneFromTwitter) { #if NET_CORE var field = timeZoneFromTwitter.GetType().GetField(timeZoneFromTwitter.ToString()); return((TimeZoneFromTwitterAttribute)CustomAttributeExtensions.GetCustomAttribute(field, typeof(TimeZoneFromTwitterAttribute))); #else var field = timeZoneFromTwitter.GetType().GetField(timeZoneFromTwitter.ToString()); return((TimeZoneFromTwitterAttribute)Attribute.GetCustomAttribute(field, typeof(TimeZoneFromTwitterAttribute))); #endif }
public static Option <T> GetAttribute <T>(this MemberInfo member, bool inherit) where T : System.Attribute { var attribute = CustomAttributeExtensions.GetCustomAttribute <T>(member, inherit); if (attribute != null) { return(new Option <T>(attribute)); } return(Option <T> .None); }
public static Option <T> GetAttribute <T>(this System.Type type, bool inherit) where T : System.Attribute { var attribute = CustomAttributeExtensions.GetCustomAttribute <T>(type.GetTypeInfo(), inherit); if (attribute != null) { return(new Option <T>(attribute)); } return(Option <T> .None); }
public void GetCustomAttribute_Multiple_NotInherited() { var attribute = CustomAttributeExtensions.GetCustomAttribute(s_typeTestClass.GetTypeInfo(), typeof(MyAttribute_AllowMultiple_Inherited), false); Assert.NotNull(attribute); var attributes = CustomAttributeExtensions.GetCustomAttributes(s_typeTestClass.GetTypeInfo(), typeof(MyAttribute_AllowMultiple_Inherited), false); Assert.Equal(1, attributes.Count()); }
public void GetCustomAttributeOfT_Single_NoInherit() { Type type = typeof(TestClass_P); MethodInfo miWithAttributes = type.GetTypeInfo().GetDeclaredMethod("methodWithAttribute"); ParameterInfo piWithAttributes = miWithAttributes.GetParameters()[0]; Attribute attribute = CustomAttributeExtensions.GetCustomAttribute <MyAttribute_Single_P>(piWithAttributes, false); Assert.NotNull(attribute); }
public void GetCustomAttributeOfT_Single() { Attribute attribute = CustomAttributeExtensions.GetCustomAttribute <MyAttribute_Single_Inherited>(s_typeTestClass.GetTypeInfo()); Assert.NotNull(attribute); Assert.Throws <AmbiguousMatchException>(() => { attribute = CustomAttributeExtensions.GetCustomAttribute <MyAttribute_AllowMultiple_Inherited>(s_typeTestClass.GetTypeInfo()); }); }
private IType GetExternalEnumeratorItemType(IType iteratorType) { Type type = ((ExternalType)iteratorType).ActualType; #if DNXCORE50 var attribute = CustomAttributeExtensions.GetCustomAttribute <EnumeratorItemTypeAttribute>(type.GetTypeInfo()); #else var attribute = (EnumeratorItemTypeAttribute)Attribute.GetCustomAttribute(type, typeof(EnumeratorItemTypeAttribute)); #endif return(null != attribute?Map(attribute.ItemType) : null); }