Exemplo n.º 1
0
        private void ComputeDependentTypes()
        {
            if (!CILType.IsPrimitive)
            {
                var fields = CILType.GetFields(
                    BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

                foreach (FieldInfo field in fields)
                {
                    bool consider = (CILType.IsValueType && !HasIntrinsicTypeOverride) ||
                                    field.HasCustomOrInjectedAttribute <DependentType>();
                    if (!consider)
                    {
                        continue;
                    }

                    object fieldVal = _sample == null ? null : field.GetValue(_sample);
                    if (fieldVal == null)
                    {
                        _memberTypes[field] = new TypeDescriptor(field.FieldType);
                    }
                    else
                    {
                        _memberTypes[field] = new TypeDescriptor(fieldVal);
                    }
                }

                var properties = CILType.GetProperties(
                    BindingFlags.Instance | BindingFlags.Public);

                foreach (PropertyInfo prop in properties)
                {
                    bool consider = prop.HasCustomOrInjectedAttribute <DependentType>();
                    if (!consider)
                    {
                        continue;
                    }

                    object propVal = _sample == null ? null : prop.GetValue(_sample, new object[0]);
                    if (propVal == null)
                    {
                        _memberTypes[prop] = new TypeDescriptor(prop.PropertyType);
                    }
                    else
                    {
                        _memberTypes[prop] = new TypeDescriptor(propVal);
                    }
                }
            }
        }
Exemplo n.º 2
0
        private TypeDescriptor GetElement0Type()
        {
            if (_sample == null)
            {
                var etype = CILType.GetElementType();
                return(etype == null ? null : new TypeDescriptor(etype));
            }

            if (CILType.IsArray)
            {
                Array array = (Array)_sample;
                if (array.Length == 0)
                {
                    return(new TypeDescriptor(CILType.GetElementType()));
                }

                object sample = array.GetValue(new int[array.Rank]);
                if (sample == null)
                {
                    return(new TypeDescriptor(CILType.GetElementType()));
                }
                else
                {
                    return(new TypeDescriptor(sample));
                }
            }
            else if (CILType.IsByRef)
            {
                return(new TypeDescriptor(_sample));
            }
            else if (Constraints.Length > 0)
            {
                var indexers = CILType.GetProperties(BindingFlags.Public | BindingFlags.Instance)
                               .Where(p => p.GetIndexParameters().Length == Constraints.Length &&
                                      p.GetIndexParameters().All(ip => ip.ParameterType == typeof(int)));
                if (indexers.Any())
                {
                    var indexer = indexers.First();
                    if (Constraints.All(c => c.Size > 0))
                    {
                        object[] index = Constraints.Select(r => (object)r.FirstBound).ToArray();
                        try
                        {
                            var indexSample = indexer.GetValue(_sample, index);
                            return(TypeDescriptor.GetTypeOf(indexSample));
                        }
                        catch (Exception)
                        {
                            return(indexer.PropertyType);
                        }
                    }
                    else
                    {
                        return(indexer.PropertyType);
                    }
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 3
0
        private void InitTypeParams()
        {
            var mtit = CILType.GetCustomOrInjectedAttribute <MapToIntrinsicType>();

            if (mtit != null)
            {
                HasIntrinsicTypeOverride = true;
                IntrinsicTypeOverride    = mtit.IntrinsicType;
            }
            else if (CILType.GetCustomOrInjectedAttribute <CompilerGeneratedAttribute>() != null)
            {
                HasIntrinsicTypeOverride = true;
                IntrinsicTypeOverride    = EIntrinsicTypes.IllegalRuntimeType;
            }
            else
            {
                HasIntrinsicTypeOverride = false;
            }

            if (CILType.IsArray)
            {
                int rank = CILType.GetArrayRank();
                TypeParams = new object[rank];
                if (_sample != null)
                {
                    Array array = (Array)_sample;
                    for (int i = 0; i < rank; i++)
                    {
                        TypeParams[i] = array.GetLength(i);
                    }
                }
            }
            else
            {
                List <object> typeParams = new List <object>();
                foreach (PropertyInfo pi in CILType.GetProperties(
                             BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
                {
                    object[] tparams = pi.GetCustomAndInjectedAttributes(typeof(TypeParameter));
                    if (tparams.Length > 0)
                    {
                        object arg = _sample == null ? null : pi.GetGetMethod().Invoke(_sample, new object[0]);
                        typeParams.Add(arg);
                    }
                }
                TypeParams = typeParams.ToArray();
            }

            if (IsComplete)
            {
                if (CILType.IsArray)
                {
                    Range[] result = new Range[Rank];
                    for (int i = 0; i < result.Length; i++)
                    {
                        result[i] = new Range(0, (int)TypeParams[i] - 1, EDimDirection.To);
                    }
                    Constraints = result;
                }
                else
                {
                    List <Range> result = new List <Range>();
                    foreach (PropertyInfo pi in CILType.GetProperties(
                                 BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
                    {
                        object[] tparams = pi.GetCustomAndInjectedAttributes(typeof(TypeParameter));
                        if (tparams.Length > 0)
                        {
                            TypeParameter tparam = (TypeParameter)tparams[0];
                            MethodInfo    miconv = tparam.RangeConverter.GetMethod("ConvertToRange",
                                                                                   BindingFlags.Static | BindingFlags.Public);
                            object arg  = pi.GetGetMethod().Invoke(_sample, new object[0]);
                            Range  carg = (Range)miconv.Invoke(null, new object[] { arg });
                            result.Add(carg);
                        }
                    }
                    Constraints = result.ToArray();
                }
            }

            Element0Type = GetElement0Type();
        }