public static TAttribute GetCustomAttribute <TAttribute>(this TypeInfo typeInfo) where TAttribute : Attribute { var attrType = typeof(TAttribute); var attributes = typeInfo.GetCustomAttributes(attrType, false); return((TAttribute)attributes.FirstOrDefault()); }
public static bool HasJsonUseTypeHintAttribute(TP tp) { #if WINDOWS_STORE return(tp.GetCustomAttribute <JsonUseTypeHintAttribute> (true) != null); #else return(tp.GetCustomAttributes(typeof(JsonUseTypeHintAttribute), true).Length != 0); #endif }
private static IEnumerable<TargetElementAttribute> GetValidTargetElementAttributes( TypeInfo typeInfo, ErrorSink errorSink) { var targetElementAttributes = typeInfo.GetCustomAttributes<TargetElementAttribute>(inherit: false); return targetElementAttributes.Where(attribute => ValidTargetElementAttributeNames(attribute, errorSink)); }
private static TestGroup CreateGroup(TypeInfo type) { TestGroup group = new TestGroup(); group.Name = type.Name; group.Tags.Add(type.Name); group.Tags.Add(type.FullName); if (type.GetCustomAttribute<FunctionalTestAttribute>(true) != null) { group.Tags.Add("Functional"); } foreach (TagAttribute attr in type.GetCustomAttributes<TagAttribute>(true)) { group.Tags.Add(attr.Tag); } return group; }
internal ClassMetadata(TypeInfo typeInfo) { #if !DataAnnotations_Missing var table = (TableAttribute)typeInfo.GetCustomAttributes(typeof(TableAttribute), true).SingleOrDefault(); if (table != null) { MappedTableName = table.Name; MappedSchemaName = table.Schema; } #endif #if Weird_Reflection var shadowingProperties = (from p in typeInfo.DeclaredProperties where IsHidingMember(p) select p).ToList(); var propertyList = typeInfo.DeclaredProperties.ToList(); #elif TypeInfo_Is_Not_Type var type = typeInfo.AsType(); var shadowingProperties = (from p in type.GetProperties() where IsHidingMember(p) select p).ToList(); var propertyList = type.GetProperties(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); #else var shadowingProperties = (from p in typeInfo.GetProperties() where IsHidingMember(p) select p).ToList(); var propertyList = typeInfo.GetProperties(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); #endif Func<PropertyInfo, bool> IsHidden = propertyInfo => !shadowingProperties.Contains(propertyInfo) && shadowingProperties.Any(p => p.Name == propertyInfo.Name); Properties = new PropertyMetadataCollection(propertyList.Where(p => !IsHidden(p)).Select(p => new PropertyMetadata(p))); //List the properties that are affected when the indicated property is modified. foreach (var property in Properties) foreach (CalculatedFieldAttribute fieldList in property.PropertyInfo.GetCustomAttributes(typeof(CalculatedFieldAttribute), true)) foreach (var field in fieldList.SourceProperties) { if (!Properties.Contains(field)) throw new InvalidOperationException(string.Format("Cannot find property {0} on type {1}. This is needed for the calculated property {2}", field, typeInfo.FullName, property.Name)); Properties[field].AddCalculatedField(property); } foreach (var property in Properties) property.EndInit(); Constructors = new ConstructorMetadataCollection(typeInfo.DeclaredConstructors); }
private static DebuggerTypeProxyAttribute GetApplicableDebuggerTypeProxyAttribute(TypeInfo type) { // includes inherited attributes. The debugger uses the first attribute if multiple are applied. var result = type.GetCustomAttributes<DebuggerTypeProxyAttribute>().FirstOrDefault(); if (result != null) { return result; } // TODO (tomat): which assembly should we look at for proxy attributes? foreach (DebuggerTypeProxyAttribute attr in type.Assembly.GetCustomAttributes<DebuggerTypeProxyAttribute>()) { if (IsApplicableAttribute(type, attr.Target.GetTypeInfo(), attr.TargetTypeName)) { return attr; } } return null; }
void SetupPart(System.Reflection.TypeInfo sourceType, BindingExpressionPart part) { part.Arguments = null; part.LastGetter = null; part.LastSetter = null; PropertyInfo property = null; if (part.IsIndexer) { if (sourceType.IsArray) { int index; if (!int.TryParse(part.Content, NumberStyles.Number, CultureInfo.InvariantCulture, out index)) { Console.WriteLine($"Binding : {part.Content} could not be parsed as an index for a {sourceType}"); } else { part.Arguments = new object[] { index } }; part.LastGetter = sourceType.GetDeclaredMethod("Get"); part.LastSetter = sourceType.GetDeclaredMethod("Set"); part.SetterType = sourceType.GetElementType(); } DefaultMemberAttribute defaultMember = sourceType.GetCustomAttributes(typeof(DefaultMemberAttribute), true).OfType <DefaultMemberAttribute>().FirstOrDefault(); string indexerName = defaultMember != null ? defaultMember.MemberName : "Item"; part.IndexerName = indexerName; #if NETSTANDARD2_0 try { property = sourceType.GetDeclaredProperty(indexerName); } catch (AmbiguousMatchException) { // Get most derived instance of property foreach (var p in sourceType.GetProperties().Where(prop => prop.Name == indexerName)) { if (property == null || property.DeclaringType.IsAssignableFrom(property.DeclaringType)) { property = p; } } } #else property = sourceType.GetDeclaredProperty(indexerName); #endif if (property == null) //is the indexer defined on the base class? { property = sourceType.BaseType?.GetProperty(indexerName); } if (property == null) //is the indexer defined on implemented interface ? { foreach (var implementedInterface in sourceType.ImplementedInterfaces) { property = implementedInterface.GetProperty(indexerName); if (property != null) { break; } } } if (property != null) { ParameterInfo parameter = property.GetIndexParameters().FirstOrDefault(); if (parameter != null) { try { object arg = Convert.ChangeType(part.Content, parameter.ParameterType, CultureInfo.InvariantCulture); part.Arguments = new[] { arg }; } catch (FormatException) { } catch (InvalidCastException) { } catch (OverflowException) { } } } } else { property = sourceType.GetDeclaredProperty(part.Content) ?? sourceType.BaseType?.GetProperty(part.Content); } if (property != null) { if (property.CanRead && property.GetMethod != null) { if (property.GetMethod.IsPublic && !property.GetMethod.IsStatic) { part.LastGetter = property.GetMethod; } } if (property.CanWrite && property.SetMethod != null) { if (property.SetMethod.IsPublic && !property.SetMethod.IsStatic) { part.LastSetter = property.SetMethod; part.SetterType = part.LastSetter.GetParameters().Last().ParameterType; if (Binding.AllowChaining) { FieldInfo bindablePropertyField = sourceType.GetDeclaredField(part.Content + "Property"); if (bindablePropertyField != null && bindablePropertyField.FieldType == typeof(BindableProperty) && sourceType.ImplementedInterfaces.Contains(typeof(IElementController))) { MethodInfo setValueMethod = null; #if NETSTANDARD1_0 foreach (MethodInfo m in sourceType.AsType().GetRuntimeMethods()) { if (m.Name.EndsWith("IElementController.SetValueFromRenderer")) { ParameterInfo[] parameters = m.GetParameters(); if (parameters.Length == 2 && parameters[0].ParameterType == typeof(BindableProperty)) { setValueMethod = m; break; } } } #else setValueMethod = typeof(IElementController).GetMethod("SetValueFromRenderer", new[] { typeof(BindableProperty), typeof(object) }); #endif if (setValueMethod != null) { part.LastSetter = setValueMethod; part.IsBindablePropertySetter = true; part.BindablePropertyField = bindablePropertyField.GetValue(null); } } } } } #if !NETSTANDARD1_0 //TupleElementNamesAttribute tupleEltNames; //if (property != null // && part.NextPart != null // && property.PropertyType.IsGenericType // && (property.PropertyType.GetGenericTypeDefinition() == typeof(ValueTuple<>) // || property.PropertyType.GetGenericTypeDefinition() == typeof(ValueTuple<,>) // || property.PropertyType.GetGenericTypeDefinition() == typeof(ValueTuple<,,>) // || property.PropertyType.GetGenericTypeDefinition() == typeof(ValueTuple<,,,>) // || property.PropertyType.GetGenericTypeDefinition() == typeof(ValueTuple<,,,,>) // || property.PropertyType.GetGenericTypeDefinition() == typeof(ValueTuple<,,,,,>) // || property.PropertyType.GetGenericTypeDefinition() == typeof(ValueTuple<,,,,,,>) // || property.PropertyType.GetGenericTypeDefinition() == typeof(ValueTuple<,,,,,,,>)) // && (tupleEltNames = property.GetCustomAttribute(typeof(TupleElementNamesAttribute)) as TupleElementNamesAttribute) != null) //{ // // modify the nextPart to access the tuple item via the ITuple indexer // var nextPart = part.NextPart; // var name = nextPart.Content; // var index = tupleEltNames.TransformNames.IndexOf(name); // if (index >= 0) // { // nextPart.IsIndexer = true; // nextPart.Content = index.ToString(); // } //} #endif } }
public static bool HasJsonUseTypeHintAttribute ( TP tp ) { #if WINDOWS_STORE return tp.GetCustomAttribute<JsonUseTypeHintAttribute> (true) != null; #else return tp.GetCustomAttributes(typeof(JsonUseTypeHintAttribute),true).Length != 0; #endif }
private void EntitySystemOnComponentTypeAdded(object sender, TypeInfo type) { var rendererTypeAttributes = type.GetCustomAttributes<DefaultEntityComponentRendererAttribute>(); foreach (var rendererTypeAttribute in rendererTypeAttributes) { var processorType = AssemblyRegistry.GetType(rendererTypeAttribute.TypeName); if (processorType == null || !typeof(IEntityComponentRenderProcessor).GetTypeInfo().IsAssignableFrom(processorType.GetTypeInfo())) { continue; } var registeredProcessors = new RegisteredRenderProcessors(processorType, VisibilityGroups.Count); registeredRenderProcessorTypes.Add(type, registeredProcessors); // Create a render processor for each visibility group foreach (var visibilityGroup in VisibilityGroups) { CreateRenderProcessor(registeredProcessors, visibilityGroup); } } }
private static TestGroup CreateGroup(TypeInfo type) { TestGroup group = new TestGroup(); group.Name = type.Name; group.Tags.Add(type.Name); group.Tags.Add(type.FullName); if (type.GetCustomAttributes(true).Where(a => a.GetType() == typeof(FunctionalTestAttribute)).Any()) { group.Tags.Add("Functional"); } foreach (TagAttribute attr in type.GetCustomAttributes(true).Where(a => a.GetType() == typeof(TagAttribute))) { group.Tags.Add(attr.Tag); } return group; }
private IEnumerable<string> GetRequiredRoles(TypeInfo screenType) { var attributes = screenType.GetCustomAttributes(typeof (RequiredRoleAttribute)).Select(x => (RequiredRoleAttribute)x); return attributes.Select(x => x.RoleName); }
private void GenerateCommands(TypeInfo commandType) { foreach (var attr in commandType.GetCustomAttributes<RunCommandAtPhaseStartAttribute>()) { if (!m_phaseCommands.ContainsKey(attr.Phase)) m_phaseCommands.Add(attr.Phase, new List<Command>()); m_phaseCommands[attr.Phase].Add(CreateCommand(commandType)); } foreach (var attr in commandType.GetCustomAttributes<RunCommandOnJoystickAttribute>()) { var button = m_buttons.OfType<JoystickButton>().Where(btn => btn.Joystick is Joystick) .FirstOrDefault(btn => (btn.Joystick as Joystick).Port == attr.ControllerId && btn.ButtonNumber == attr.ButtonId); if (button == null) { m_buttons.Add(button = new JoystickButton(new Joystick(attr.ControllerId), attr.ButtonId)); } AttachCommandToButton(commandType, button, attr.ButtonMethod); } foreach (var attr in commandType.GetCustomAttributes<RunCommandOnNetworkKeyAttribute>()) { var button = m_buttons.OfType<NetworkButton>().FirstOrDefault(btn => btn.SourceTable == NetworkTable.GetTable(attr.TableName) && btn.Field == attr.Key); if(button == null) { m_buttons.Add(button = new NetworkButton(attr.TableName, attr.Key)); } AttachCommandToButton(commandType, button, attr.ButtonMethod); } }
private static IEnumerable<KeyValuePair<Subsystem, string>> EnumerateGeneratedSubsystems(TypeInfo subsystemType) { foreach (var attr in subsystemType.GetCustomAttributes<ExportSubsystemAttribute>()) { var subsystem = (Subsystem)Activator.CreateInstance(subsystemType); if (attr.DefaultCommandType != null && subsystem is Subsystem) { var defaultCommandType = attr.DefaultCommandType; if (!typeof(Command).IsAssignableFrom(defaultCommandType)) { throw new IllegalUseOfCommandException("Default command type is not an attributed commmand."); } var defaultCommand = (Command)Activator.CreateInstance(defaultCommandType, subsystem); if (!defaultCommand.DoesRequire(subsystem)) { defaultCommand.Requires(subsystem); } subsystem.SetDefaultCommand(defaultCommand); } yield return new KeyValuePair<Subsystem, string>(subsystem, attr.Name); } }
void SetupPart(TypeInfo sourceType, BindingExpressionPart part) { part.Arguments = null; part.LastGetter = null; part.LastSetter = null; PropertyInfo property = null; if (part.IsIndexer) { if (sourceType.IsArray) { int index; if (!int.TryParse(part.Content, out index)) Log.Warning("Binding", "{0} could not be parsed as an index for a {1}", part.Content, sourceType); else part.Arguments = new object[] { index }; part.LastGetter = sourceType.GetDeclaredMethod("Get"); part.LastSetter = sourceType.GetDeclaredMethod("Set"); part.SetterType = sourceType.GetElementType(); } DefaultMemberAttribute defaultMember = sourceType.GetCustomAttributes(typeof(DefaultMemberAttribute), true).OfType<DefaultMemberAttribute>().FirstOrDefault(); string indexerName = defaultMember != null ? defaultMember.MemberName : "Item"; part.IndexerName = indexerName; property = sourceType.GetDeclaredProperty(indexerName); if (property == null) property = sourceType.BaseType.GetProperty(indexerName); if (property != null) { ParameterInfo parameter = property.GetIndexParameters().FirstOrDefault(); if (parameter != null) { try { object arg = Convert.ChangeType(part.Content, parameter.ParameterType, CultureInfo.InvariantCulture); part.Arguments = new[] { arg }; } catch (FormatException) { } catch (InvalidCastException) { } catch (OverflowException) { } } } } else { property = sourceType.GetDeclaredProperty(part.Content); if (property == null) property = sourceType.BaseType.GetProperty(part.Content); } if (property != null) { if (property.CanRead && property.GetMethod.IsPublic && !property.GetMethod.IsStatic) part.LastGetter = property.GetMethod; if (property.CanWrite && property.SetMethod.IsPublic && !property.SetMethod.IsStatic) { part.LastSetter = property.SetMethod; part.SetterType = part.LastSetter.GetParameters().Last().ParameterType; if (Binding.AllowChaining) { FieldInfo bindablePropertyField = sourceType.GetDeclaredField(part.Content + "Property"); if (bindablePropertyField != null && bindablePropertyField.FieldType == typeof(BindableProperty) && sourceType.ImplementedInterfaces.Contains(typeof(IElementController))) { MethodInfo setValueMethod = null; foreach (MethodInfo m in sourceType.AsType().GetRuntimeMethods()) { if (m.Name.EndsWith("IElementController.SetValueFromRenderer")) { ParameterInfo[] parameters = m.GetParameters(); if (parameters.Length == 2 && parameters[0].ParameterType == typeof(BindableProperty)) { setValueMethod = m; break; } } } if (setValueMethod != null) { part.LastSetter = setValueMethod; part.IsBindablePropertySetter = true; part.BindablePropertyField = bindablePropertyField.GetValue(null); } } } } } }