I have this Class which is accepting a Dictionary<string,object> which is been invoked from different places in the code. Dictionary concept is making things untyped and difficult to figure out what to pass the class to function otherwise it throws run-time exception. As this Dictionary<string,object> is a contract definition so i had to write an extension method to convert my type to a dictionary when invoking my class . But now how can i close this Class that it only accepts a specific Type
So for eg.
public class CreateReport : IRep
{
public void SetParam(Dictionary<string,object> parm)
{
// Here the dictionary param are been set.
}
public object RunRep()
{
}
}
ClassInvoker.Invoke(CreateReport , Dictionary<string,object>{"MyParam" , "World"});
So this is how things are now.
I have changed it by creating a Property class as
public class CreateReportProp
{
public string MyParam { get;set;}
}
and having a extension method as ConvertObjToDict
so Now we have to do something like
ClassInvoker.Invoke(CreateReport , new CreateReportProp { MyParam = "World"}.ConvertObjToDict());
But i would like to go further and close the Class so that CreateReportClass you can only Pass CreateReportParam otherwise compiler throws an exception.
Please give me some ideas as how can i acheive this.
I presume you are using the CreateReport class for many specific reports? If this is the case, the you have a chance to enact a compile-time check. If the CreateReport class dynamically invokes the report, i.e. by name, and each report has different parameter requirements, then there is no compile-time check possible.
In the first case, you would change the signature for the CreateReport class as follows:
In the SetParam method, you would have
For each specific report you can create a different property class as long as it inherits from CreateReportProp. Then when you instance the report class you will specify that one.
If you wish to do less changes within the CreateReport class you can have your CreateReportProp implement an IEnumerable or IDictionary interface so that iterating the parameters is more generic.