コード例 #1
0
ファイル: TypeDescriptor.cs プロジェクト: cephdon/systemsharp
        /// <summary>
        /// Creates a sample instance of the described type.
        /// </summary>
        /// <param name="options">creation options</param>
        public object GetSampleInstance(ETypeCreationOptions options = ETypeCreationOptions.AnyObject)
        {
            Contract.Requires(
                !(options.HasFlag(ETypeCreationOptions.AdditiveNeutral) && options.HasFlag(ETypeCreationOptions.MultiplicativeNeutral)) &&
                !(options.HasFlag(ETypeCreationOptions.AdditiveNeutral) && options.HasFlag(ETypeCreationOptions.NonZero)) &&
                !(options.HasFlag(ETypeCreationOptions.MultiplicativeNeutral) && options.HasFlag(ETypeCreationOptions.NonZero)));

            if (IsArtificial)
            {
                throw new InvalidOperationException("Cannot construct a sample instance from an artificial type");
            }

            if (options.HasFlag(ETypeCreationOptions.AdditiveNeutral) ||
                options.HasFlag(ETypeCreationOptions.MultiplicativeNeutral) ||
                options.HasFlag(ETypeCreationOptions.NonZero))
            {
                var atf = CILType.GetCustomOrInjectedAttribute <IAlgebraicTypeFactory>();
                return(atf.CreateInstance(options, _sample));
            }

            if (_sample != null)
            {
                return(_sample);
            }

            if (options.HasFlag(ETypeCreationOptions.ReturnNullIfUnavailable))
            {
                return(null);
            }

            if (!options.HasFlag(ETypeCreationOptions.ForceCreation) && (IsConstrained || !IsStatic))
            {
                throw new InvalidOperationException("Cannot construct sample instance from constrained incomplete or non-static type");
            }

            object inst = Activator.CreateInstance(CILType);

            return(inst);
        }
コード例 #2
0
ファイル: TypeDescriptor.cs プロジェクト: cephdon/systemsharp
        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();
        }