Exemplo n.º 1
0
 internal FastMember(FastType declaring, MemberInfo member, FastMember baseMember)
 {
     DeclaringType = declaring;
     Name          = member.Name;
     Member        = member;
     BaseMember    = baseMember ?? this;
     if (member is FieldInfo)
     {
         var pi = (FieldInfo)member;
         IsField        = true;
         Type           = FastType.GetType(pi.FieldType);
         IsPublic       = pi.IsPublic;
         IsPublicSetter = pi.IsPublic;
         CanSet         = !pi.IsLiteral;
         IsStatic       = pi.IsStatic;
     }
     else
     {
         var pi = (PropertyInfo)member;
         Type           = FastType.GetType(pi.PropertyType);
         IsPublic       = pi.GetMethod.IsPublic;
         IsPublicSetter = pi.SetMethod?.IsPublic ?? false;
         IsField        = false;
         CanSet         = pi.SetMethod != null;
         IsStatic       = pi.GetMethod.IsStatic;
     }
     if (!DeclaringType.IsGenericMeta)
     {
         InitializeAccessor();
         InitializeStructAccessor();
     }
 }
Exemplo n.º 2
0
 /// <summary>
 /// Gets the <see cref="FastType"/> associated with <paramref name="type"/> type.
 /// </summary>
 public static FastType GetType(Type type)
 {
     if (type == null)
     {
         return(null);
     }
     lock (sReflectCache)
     {
         FastType result;
         if (!sReflectCache.TryGetValue(type, out result))
         {
             result = new FastType();
             sReflectCache[type] = result;
             result.Initialize(type);
         }
         return(result);
     }
 }
Exemplo n.º 3
0
 internal FastMember(MemberInfo member)
 {
     Name   = member.Name;
     Member = member;
     if (member is FieldInfo)
     {
         var pi = (FieldInfo)member;
         IsField  = true;
         Type     = FastType.GetType(pi.FieldType);
         IsPublic = pi.IsPublic;
         CanSet   = !pi.IsLiteral;
         IsStatic = pi.IsStatic;
     }
     else
     {
         var pi = (PropertyInfo)member;
         Type     = FastType.GetType(pi.PropertyType);
         IsPublic = pi.GetMethod.IsPublic;
         IsField  = false;
         CanSet   = pi.SetMethod != null;
         IsStatic = pi.GetMethod.IsStatic;
     }
     SetMember(member);
 }
Exemplo n.º 4
0
        public static void ResolveProperties(ITypeTree <ServiceDefinition> services, ITypeTree <object> scope, object instance)
        {
            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }
            if (scope == null)
            {
                scope = new TypeTree <object>();
            }

            var ftype = FastType.GetType(instance.GetType());
            var props = (
                from p in ftype.GetRuntimeMembers()
                where !p.IsStatic
                let pi = p.Member.GetCustomAttributes <ImportAttribute>().FirstOrDefault()
                         where pi != null
                         select new { p, pi }
                ).ToList();

            foreach (var item in props)
            {
                var p  = item.p;
                var pi = item.pi;
                if (IsBaseClass(typeof(IEnumerable), p.Type.Type))
                {
                    var t = pi.ImportedType;
                    // T[]
                    if (p.Type.Type.IsArray)
                    {
                        if (t == null)
                        {
                            t = p.Type.Type.GetElementType();
                        }
                        if (!IsBaseClass(p.Type.Type.GetElementType(), t))
                        {
                            throw new NotSupportedException($"Property {instance.GetType().Name}.{p.Name}, can't import {t.Name}");
                        }
                        var values = MergeKeys(services, scope, t).Select(x => SolveExact(services, scope, x)).ToList();
                        var prop   = Array.CreateInstance(t, values.Count);
                        for (int i = 0; i < values.Count; i++)
                        {
                            prop.SetValue(values[i], i);
                        }
                        item.p.SetValue(instance, prop);
                    }
                    // List<T>
                    else
                    {
                        if (!IsBaseClass(typeof(IList), p.Type.Type))
                        {
                            throw new InvalidOperationException($"[Import] property {instance.GetType().Name}.{p.Name} must be an array or an IList");
                        }

                        if (t == null)
                        {
                            var ga = p.Type.Type.GenericTypeArguments;
                            if (ga.Length != 1)
                            {
                                throw new NotSupportedException($"[Import] property {instance.GetType().Name}.{p.Name} must be generic or the Import type must be defined");
                            }
                            t = ga[0];
                        }
                        var value = p.GetValue(instance);
                        if (value == null)
                        {
                            if (!CanBeInstantiated(p.Type.Type) || !p.CanSet)
                            {
                                throw new InvalidOperationException($"Can't [Import]{p.Type.Type.Name} for {instance.GetType().Name}.{p.Name}");
                            }
                            value = Create(services, scope, p.Type.Type);
                            p.SetValue(instance, value);
                        }
                        var list   = (IList)value;
                        var values = MergeKeys(services, scope, t).Select(x => SolveExact(services, scope, x)).ToList();
                        foreach (var x in values)
                        {
                            list.Add(x);
                        }
                    }
                }
                else
                {
                    // simple property
                    var o = ResolveSingle(services, scope, pi.ImportedType ?? p.Type.Type);
                    item.p.SetValue(instance, o);
                }
            }
        }