예제 #1
0
        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);
        }
예제 #2
0
        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();
        }
예제 #3
0
        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);
        }
예제 #4
0
        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;
        }