0

I need something like Dictionary where dynamic can be anything from string to objects.

But when i use objects, i need to know the type of the object and then access the appropriate properties of those objects.

Is there a way WITHOUT using Reflection.

* EDITED **

I tried to use this :

CloneObject<T, TU>(IDictionary<T, TU> sourceObject)

But if i use this, how can i access T's public fields without using reflection

Saravanan
  • 7,637
  • 5
  • 41
  • 72
  • A Dictionary *is* a generic collection of keys and values. What do you want to use this for? Can you give an example? – Brian Kintz Jul 15 '11 at 08:33
  • Also, if you want people to see your question, it's a good idea to tag it with the language you're using. – Brian Kintz Jul 15 '11 at 08:37
  • @elmugrat: thanks for the tagging. I have updated the question too. I think that this is not possible to acheive. – Saravanan Jul 15 '11 at 08:39
  • @saravanan: Can you clarify your question further, possibly with some samples of - ideally - how you would like this to work, and what you would use it for. I still have no idea what you're asking for, sorry. – Binary Worrier Jul 15 '11 at 08:41
  • So your trying to write a method to clone any Dictionary? If so, then see here: http://stackoverflow.com/questions/139592/what-is-the-best-way-to-clone-deep-copy-a-net-generic-dictionarystring-t – Brian Kintz Jul 15 '11 at 08:44
  • Why do you want this? This sounds like something that might well be simpler to solve by avoiding the need in the first place. – Eamon Nerbonne Jul 15 '11 at 09:24

3 Answers3

1

You can use Hashtable for this purpose

Here is the Examples

http://www.dotnetperls.com/hashtable

You can also use Dictionary which is more efficient than Hashtable

See Examples Here:

http://www.dotnetperls.com/dictionary-keys

SMK
  • 2,098
  • 2
  • 13
  • 21
0

I'm confused a little bit. You trying to store any types of objects in your dictionary but access to them without reflection.

If so you can use dynamic types:

Dictionary dict = new Dictionary();

dict["string"] = "some string";
dict["int"] = 25;
dict["my_class"] = new MyClass {SomeProperty = 12};

And then you can access all this values without any casts:

string s1 = dict["string"].Substring(0, 4); // s1 equals to "some"
int propertyValue = dict["my_class"].SomeProperty; // propertyValue equals to 12

where MyClass is:

class MyClass
{
  public int SomeProperty {get;set;}
}
Sergey Teplyakov
  • 11,477
  • 34
  • 49
0

Without using reflection, this task cannot be completed. All I have done is create clones of objects separately and then used them.

Saravanan
  • 7,637
  • 5
  • 41
  • 72