I'm not quite sure the correct way of asking this question, so I'm just going to go for it.
I am looking for the cleanest way of passing in "parameter sets" into a Method. For instance:
Dictionary<string, string> Parameters = new Dictionary<string, string>();
Parameters.Add("UserID", "12");
Parameters.Add("SiteID", "43");
CreateLog("Hello World!", Parameters);
public void CreateLog(string Message, Dictionary<string, string> Parameters)
{
...
}
However, I would like to do this in a much cleaner way and I'm sure there is some method of doing it that I can't think of. I could make a custom object that takes a string and an object, for instance:
public void CreateLog(string Message, params LogParameter[] Parameters)
{
...
}
Then I could create a new Log Parameter for each Parameter and pass it in, but I would like to be a bit more concise than that and avoid having to create all those new objects for something that (I think) should be simpler.
One possible solution is to do something like this:
CreateLog("Hello World!", new { UserID = 12, SiteID = 43 });
public void CreateLog(string Message, object Parameters)
{
...
}
This is similar to how C# ASP.NET creates URL variables/how DevExpress creates callbacks for their controls. But how do they get the values out of the object in the method?
Any thoughts?
new { UserID = 12, SiteID = 43 }is still creating an object, it's just the type happens to be anonymous. You will probably end up doing more work inCreateLog, though, trying to access its properties."How to get value out of the object in Method?". This will be done via ReflectionParameters.GetType().GetAllProperties()and get the value and name of each property with a foreach loop.