How to implement class with collection of string/object pairs so that an object can be returned with generic method?

The only way I can think of doing this is to have Dictionary parameters = new Dictionary(); // cast a string parameters. Add("DatabaseName", "SomeBase"); // cast a list parameters. Add("Classes", new List { int.

Parse("11"), int. Parse("12"), int. Parse("13") }); // cast an integer parameters.

Add("IntValue", int. Parse("3")); // cast a double parameters. Add("DoubleValue", Double.

Parse("4.0")) Then when you want to use the values you just do the unboxing: int intValue = (int)parameters"IntValue"; Double doubleValue = (Double)parameters"DoubleValue"; List classes = (List)parameters"Classes"; // etc As I mentioned before: after doing performance testing I found that unboxing has negligent overhead so you should not see any noticeable performance issues Update 2.0: I'm guessing you want to automatically convert the type without having to explicitly specify it when you're adding it into the settings dictionary. It should work if your dictionary's value is an Object : Type t = typeof(double); Object val = Convert. ChangeType("2.0", t); // you still need to unbox the value to use it double actual = (double)val So your example should work: settingskVPair.

Key = Convert. ChangeType(value, type) You just need to unbox the value with the correct type when you're going to use it.

The only way I can think of doing this is to have Dictionary and then cast the Object to the appropriate type. Your underlying problem is how to dynamically specify the type: stackoverflow.com/questions/2466278/dyna... Turns out that type casting (actually unboxing) in C# has very little overhead: stackoverflow.com/questions/2466629/c-pe... Update: Here is how you do the casting: Dictionary parameters = new Dictionary(); // cast a string parameters. Add("DatabaseName", "SomeBase"); // cast a list parameters.

Add("Classes", new List { int. Parse("11"), int. Parse("12"), int.

Parse("13") }); // cast an integer parameters. Add("IntValue", int. Parse("3")); // cast a double parameters.

Add("DoubleValue", Double. Parse("4.0")); Then when you want to use the values you just do the unboxing: int intValue = (int)parameters"IntValue"; Double doubleValue = (Double)parameters"DoubleValue"; List classes = (List)parameters"Classes"; // etc... As I mentioned before: after doing performance testing I found that unboxing has negligent overhead so you should not see any noticeable performance issues. Update 2.0: I'm guessing you want to automatically convert the type without having to explicitly specify it when you're adding it into the _settings dictionary.It should work if your dictionary's value is an Object: Type t = typeof(double); Object val = Convert.

ChangeType("2.0", t); // you still need to unbox the value to use it double actual = (double)val; So your example should work: _settingskVPair. Key = Convert. ChangeType(value, type); You just need to unbox the value with the correct type when you're going to use it.

Exactly what I was thinking:) – Veer Apr 6 '10 at 14:23 sorry. I was not able to use arrowheads and my example was total BS. Now squarebrackets – matti Apr 6 '10 at 14:28 @matti formatting issues?

I didn't notice any problems. Anyway, I hope that my answer makes sense... let me know if you want me to clarify something. – Lirik Apr 6 '10 at 15:14 @lirik: and you please check the new code in my edited question.

Don't know how to cast the string read from file to type... – matti Apr 6 '10 at 15:53 I have KeyValuePair kVPair and I want to cast string value to kVPair.Value. How to do this? – matti Apr 6 '10 at 16:00.

You don't have to do this with generics. You could simply have three methods like public string GetStringConfig(string key){} public List GetListConfig(string key){} public int GetIntegerConfig(string key){} (you would have to throw an error if you try to parse / convert a string that does not match) The above is like using a SQLAdapter. This does of course assume you know the type of object the key should return.

That is true. But it limits the types. What if someone wants to use a date?

Then this class has to be changed. – matti Apr 6 '10 at 14:35 I had made the assumption that you had a finite list of types a user could have as values. You are right a new method would have to be added if you wanted to accept dates.

Wouldn't you still have to write some sort of check / conversion method for the string object to be converted to the desired type anyway? – Tim Jarvis Apr 6 '10 at 15:16 If you did go this way, convention is to use the BCL type name, so GetInt32Config, not GetIntegerConfig. – Marc Gravell?

Apr 6 '10 at 15:40 you're right. This whole thing was not so good idea :( – matti Apr 6 '10 at 15:51.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions