/// <summary> /// Convert excel range parameter to arrays and null objects to NA /// </summary> /// <returns></returns> private ParameterConversionConfiguration GetParameterConversionConfig() { var objectRepository = Container.GetInstance <IObjectRepository>(); var paramConversionConfig = new ParameterConversionConfiguration() // Register the Standard Parameter Conversions (with the optional switch on how to treat references to empty cells) .AddParameterConversion(ParameterConversions.GetNullableConversion(treatEmptyAsMissing: false)) .AddParameterConversion(ParameterConversions.GetOptionalConversion(treatEmptyAsMissing: false)) // This parameter conversion adds support for string[] parameters (by accepting object[] instead). // It uses the TypeConversion utility class defined in ExcelDna.Registration to get an object->string // conversion that is consist with Excel (in this case, Excel is called to do the conversion). .AddParameterConversion((object[] inputs) => inputs.Select(TypeConversion.ConvertToInt32).ToArray()) .AddParameterConversion((object[] inputs) => inputs.Select(TypeConversion.ConvertToString).ToArray()) // #ParameterConversion Convert handle to public object .AddParameterConversion((object obj) => objectRepository.Get((string)obj)) //.AddParameterConversion((string handle) => handle.Contains("::") ? Container.GetInstance<ICreator>().Create(handle) : handle ) //.AddParameterConversion((Type type, ExcelParameterRegistration paramReg) => // (Expression<Func<object, IPublicObject>>)(obj => creator.Create((string)obj)), typeof(IPublicObject)) //paramReg.ArgumentAttribute.Name == "oTransaction" ? (Expression<Func<object, IPublicObject>>)(obj => creator.Create((string)obj)) : null, typeof(IPublicObject)) //(Expression<Func<object,IPublicObject>>) (obj => paramReg.ArgumentAttribute.Name == "hTransaction" ? creator.Create((string) obj) : null),typeof(IPublicObject)) .AddReturnConversion((object obj) => obj.IsDefault() ? ExcelError.ExcelErrorNA : obj) // #ReturnConversion Convert public objects to its handle for excel display .AddReturnConversion((IPublicObject obj) => obj.Handle); return(paramConversionConfig); }
static ParameterConversionConfiguration GetParameterConversionConfig() { // NOTE: The parameter conversion list is processed once per parameter. // Parameter conversions will apply from most inside, to most outside. // So to apply a conversion chain like // string -> Type1 -> Type2 // we need to register in the (reverse) order // Type1 -> Type2 // string -> Type1 // // (If the registration were in the order // string -> Type1 // Type1 -> Type2 // the parameter (starting as Type2) would not match the first conversion, // then the second conversion (Type1 -> Type2) would be applied, and no more, // leaving the parameter having Type1 (and probably not eligible for Excel registration.) // // // Return conversions are also applied from most inside to most outside. // So to apply a return conversion chain like // Type1 -> Type2 -> string // we need to register the ReturnConversions as // Type1 -> Type2 // Type2 -> string // var paramConversionConfig = new ParameterConversionConfiguration() // Register the Standard Parameter Conversions (with the optional switch on how to treat references to empty cells) .AddParameterConversion(ParameterConversions.GetOptionalConversion(treatEmptyAsMissing: true)) // Register some type conversions (not the ordering discussed above) .AddParameterConversion((string value) => new TestType1(value)) .AddParameterConversion((TestType1 value) => new TestType2(value)) // This is a conversion applied to the return value of the function .AddReturnConversion((TestType1 value) => value.ToString()) .AddReturnConversion((Complex value) => new double[2] { value.Real, value.Imaginary }) // .AddParameterConversion((string value) => convert2(convert1(value))); // This parameter conversion adds support for string[] parameters (by accepting object[] instead). // It uses the TypeConversion utility class defined in ExcelDna.Registration to get an object->string // conversion that is consist with Excel (in this case, Excel is called to do the conversion). .AddParameterConversion((object[] inputs) => inputs.Select(TypeConversion.ConvertToString).ToArray()) // This is a pair of very generic conversions for Enum types .AddReturnConversion((Enum value) => value.ToString(), handleSubTypes: true) .AddParameterConversion(ParameterConversions.GetEnumStringConversion()) .AddParameterConversion((object[] input) => new Complex(TypeConversion.ConvertToDouble(input[0]), TypeConversion.ConvertToDouble(input[1]))) .AddNullableConversion(treatEmptyAsMissing: true, treatNAErrorAsMissing: true); return(paramConversionConfig); }
static ParameterConversionConfiguration GetParameterConversionConfig() { var paramConversionConfig = new ParameterConversionConfiguration() // Register the Standard Parameter Conversions (with the optional switch on how to treat references to empty cells) .AddParameterConversion(ParameterConversions.GetNullableConversion(treatEmptyAsMissing: false)) .AddParameterConversion(ParameterConversions.GetOptionalConversion(treatEmptyAsMissing: false)) // From string to Type .AddParameterConversion((string input) => ToType(input)) // This parameter conversion adds support for string[] parameters (by accepting object[] instead). // It uses the TypeConversion utility class defined in ExcelDna.Registration to get an object->string // conversion that is consist with Excel (in this case, Excel is called to do the conversion). .AddParameterConversion((object[] inputs) => inputs.Select(TypeConversion.ConvertToString).ToArray()) .AddParameterConversion(RangeParameterConversion.ParameterConversion); return(paramConversionConfig); }