Reputation: 24057
Somewhere inside my function I need to do something like that:
smsg["isin"].set(ii.ISIN);
smsg["client_code"].set(Constants.CLIENT_CODE);
smsg["type"].set(1);
smsg["dir"].set(order.Operation == Side.Buy ? 1 : 2);
smsg["amount"].set(order.Lots);
smsg["price"].set(textPrice);
smsg["ext_id"].set(0);
set
method has a lot of overloads it can accept int
, string
, boolean
, DateTime
etc. totally about 15 methods.
After refactoring I decided function to use just list of parameters ignoring other variables order
ii
etc. The problem is that I don't know how can I transfer this parameters via function arguments
public uint ExecuteTransaction(Dictionary<string, object> parameters)
{
....
foreach (var parameter in parameters)
{
smsg[parameter.Key].set(parameter.Value); // compile time error!
}
Compiler doesn't know which overload to use and so I have such error:
The best overloaded method match has some invalid arguments
My Dictionary contains appropiate value for each parameter. So boolean parameters contain boolean value etc. That's why I declared Dictionary to contain general type object
.
Thanks for your help!
Upvotes: 0
Views: 139
Reputation: 2086
Well you can...
set
to accept an object parameter and let the class manage the types.foreach
block the logic.Example:
foreach (var parameter in parameters)
{
// int example
if (parameter.Value as int? != null)
smsg[parameter.Key].set((int)parameter.Value); // No error!
}
Upvotes: 1
Reputation: 5833
You should implement the set(object value) method which will determine parameter type and call typed set(T value). This is only way to use set in this maner.
Upd: if you haven't access to set as it a library you can write extension method
public static class Ext
{
public static void set(this YOUR_LIB_TYPE lib, object value)
{
if(value is int)
{
lib.set((int) value);
}
else if(value is string)
{
lib.set((string) value);
}
...
}
}
Upvotes: 0