ICSharpCode.NRefactory.CSharp.Resolver.CSharpResolver.GetDefaultValue C# (CSharp) Method

GetDefaultValue() public static method

public static GetDefaultValue ( IType type ) : object
type IType
return object
		public static object GetDefaultValue(IType type)
		{
			ITypeDefinition typeDef = type.GetDefinition();
			if (typeDef == null)
				return null;
			if (typeDef.Kind == TypeKind.Enum) {
				typeDef = typeDef.EnumUnderlyingType.GetDefinition();
				if (typeDef == null)
					return null;
			}
			switch (typeDef.KnownTypeCode) {
				case KnownTypeCode.Boolean:
					return false;
				case KnownTypeCode.Char:
					return '\0';
				case KnownTypeCode.SByte:
					return (sbyte)0;
				case KnownTypeCode.Byte:
					return (byte)0;
				case KnownTypeCode.Int16:
					return (short)0;
				case KnownTypeCode.UInt16:
					return (ushort)0;
				case KnownTypeCode.Int32:
					return 0;
				case KnownTypeCode.UInt32:
					return 0U;
				case KnownTypeCode.Int64:
					return 0L;
				case KnownTypeCode.UInt64:
					return 0UL;
				case KnownTypeCode.Single:
					return 0f;
				case KnownTypeCode.Double:
					return 0.0;
				case KnownTypeCode.Decimal:
					return 0m;
				default:
					return null;
			}
		}
		#endregion

Usage Example

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());
        }
All Usage Examples Of ICSharpCode.NRefactory.CSharp.Resolver.CSharpResolver::GetDefaultValue
CSharpResolver