public ReflectedType(Type typeToReflect) { Reflected = typeToReflect ?? throw new ArgumentNullException(nameof(typeToReflect)); _attributes = new Lazy <IReadOnlyCollection <Attribute> >(getAttributes); _properties = new Lazy <IReadOnlyCollection <ReflectedProperty> >(getProperties); _propertiesByName = new Lazy <IReadOnlyDictionary <string, ReflectedProperty> >(() => getPropertiesByName(_properties)); List <Attribute> getAttributes() => ReflectionHelper.GetAttributes(Reflected).ToList(); List <ReflectedProperty> getProperties() => ReflectionHelper .FindPublicProperties(Reflected) .Select(pi => new ReflectedProperty(pi)) .ToList(); Dictionary <string, ReflectedProperty> getPropertiesByName(Lazy <IReadOnlyCollection <ReflectedProperty> > props) => props.Value.ToDictionary(rp => rp.Name); }
public ReflectedProperty(PropertyInfo propToReflect) { Reflected = propToReflect ?? throw new ArgumentNullException(nameof(propToReflect)); _attributes = new Lazy <IReadOnlyCollection <Attribute> >(getAttributes); #if USE_CODE_GEN _getter = new Lazy <Func <object, object> >(() => Reflected.GetValueGetter()); _setter = new Lazy <Action <object, object> >(() => Reflected.GetValueSetter()); #else _getter = new Lazy <Func <object, object> >(() => instance => Reflected.GetValue(instance, null)); _setter = new Lazy <Action <object, object> >(() => (instance, value) => Reflected.SetValue(instance, value, null)); // This is slow, but there is an alternative: we can avoid codegen (voor iOS) when not dealing with netstandard 1.1 // using (Func<object,object>)Delegate.CreateDelegate(typeof(Func<object,object>), Reflected.GetGetMethod()) #endif List <Attribute> getAttributes() => ReflectionHelper.GetAttributes(Reflected).ToList(); }
internal static T GetAttribute <T>(MemberInfo t, FhirRelease version) where T : Attribute { return(ReflectionHelper.GetAttributes <T>(t).LastOrDefault(isRelevant)); bool isRelevant(Attribute a) => !(a is IFhirVersionDependent vd) || vd.AppliesToVersion(version); }
private static T getAttribute <T>(Type t, string version) where T : Attribute { return(ReflectionHelper.GetAttributes <T>(t.GetTypeInfo()).LastOrDefault(isRelevant)); bool isRelevant(Attribute a) => a is IFhirVersionDependent vd?vd.AppliesToVersion(version) : true; }