ExcelDna.Registration.TypeConversion.GetDefault C# (CSharp) Метод

GetDefault() публичный статический Метод

public static GetDefault ( Type type ) : object
type System.Type
Результат object
        public static object GetDefault(Type type)
        {
            if (type.IsValueType)
            {
                return Activator.CreateInstance(type);
            }
            return null;
        }

Usage Example

        static LambdaExpression OptionalConversion(Type type, ExcelParameterRegistration paramReg, bool treatEmptyAsMissing, bool treatNAErrorAsMissing)
        {
            // Decide whether to return a conversion function for this parameter
            if (!paramReg.CustomAttributes.OfType <OptionalAttribute>().Any())
            {
                return(null);
            }

            var defaultAttribute = paramReg.CustomAttributes.OfType <DefaultParameterValueAttribute>().FirstOrDefault();
            var defaultValue     = defaultAttribute == null?TypeConversion.GetDefault(type) : defaultAttribute.Value;

            // var returnType = type.GetGenericArguments()[0]; // E.g. returnType is double

            // Consume the attributes
            paramReg.CustomAttributes.RemoveAll(att => att is OptionalAttribute);
            paramReg.CustomAttributes.RemoveAll(att => att is DefaultParameterValueAttribute);

            // Here's the actual conversion function
            var input = Expression.Parameter(typeof(object), "input");

            return
                (Expression.Lambda(
                     Expression.Condition(
                         MissingTest(input, treatEmptyAsMissing, treatNAErrorAsMissing),
                         Expression.Constant(defaultValue, type),
                         TypeConversion.GetConversion(input, type)),
                     input));
        }