GetDefaultValue() public static method

public static GetDefaultValue ( IType type ) : object
type IType
return object
Esempio n. 1
0
        public IList <ResolveResult> GetPositionalArguments(ITypeResolveContext context)
        {
            List <ResolveResult> result = new List <ResolveResult>();

            if (positionalArguments != null)
            {
                foreach (var arg in positionalArguments)
                {
                    result.Add(Resolve(arg, context));
                }
            }
            if (namedCtorArguments == null || namedCtorArguments.Count == 0)
            {
                // no namedCtorArguments: just return the positionalArguments
                return(result.AsReadOnly());
            }
            // we do have namedCtorArguments, which need to be re-ordered and appended to the positional arguments
            IMethod method = ResolveConstructor(context);

            if (method != null)
            {
                for (int i = result.Count; i < method.Parameters.Count; i++)
                {
                    IParameter p     = method.Parameters[i];
                    bool       found = false;
                    foreach (var pair in namedCtorArguments)
                    {
                        if (pair.Key == p.Name)
                        {
                            result.Add(Resolve(pair.Value, context));
                            found = true;
                        }
                    }
                    if (!found)
                    {
                        // add the parameter's default value:
                        if (p.DefaultValue != null)
                        {
                            result.Add(Resolve(p.DefaultValue, context));
                        }
                        else
                        {
                            IType type = p.Type.Resolve(context);
                            result.Add(new ConstantResolveResult(type, CSharpResolver.GetDefaultValue(type)));
                        }
                    }
                }
            }
            return(result.AsReadOnly());
        }
Esempio n. 2
0
        public IList <IConstantValue> GetPositionalArguments(ITypeResolveContext context)
        {
            if (namedCtorArguments == null || namedCtorArguments.Count == 0)
            {
                // no namedCtorArguments: just return the positionalArguments
                if (positionalArguments != null)
                {
                    return(new ReadOnlyCollection <IConstantValue>(positionalArguments));
                }
                else
                {
                    return(EmptyList <IConstantValue> .Instance);
                }
            }
            // we do have namedCtorArguments, which need to be re-ordered and appended to the positional arguments
            List <IConstantValue> result = new List <IConstantValue>(this.positionalArguments);
            IMethod method = ResolveConstructor(context);

            if (method != null)
            {
                for (int i = result.Count; i < method.Parameters.Count; i++)
                {
                    IParameter p     = method.Parameters[i];
                    bool       found = false;
                    foreach (var pair in namedCtorArguments)
                    {
                        if (pair.Key == p.Name)
                        {
                            result.Add(pair.Value);
                            found = true;
                        }
                    }
                    if (!found)
                    {
                        // add the parameter's default value:
                        result.Add(p.DefaultValue ?? new SimpleConstantValue(p.Type, CSharpResolver.GetDefaultValue(p.Type.Resolve(context))));
                    }
                }
            }
            return(result.AsReadOnly());
        }