Click here to Skip to main content
16,014,095 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i'm working with a class that inherits from a dynamic object , in this class I've created a function that can cast to any type of object in which the code goes like this:
C#
foreach (var tmpObject in _Props)
{
    String TmpString=GetKey(tmpObject.Key,BdInfo);
   _Props[tmpObject.Key] = Convert.ChangeType(rst.Fields[TmpString].Value, tmpObject.Value.GetType()); 
}

the rst variable is just an object where the database data is stored. When executing this function, I get an error on the Convert.changeType. Is there another way to create a generic cast based on the type of object

What I have tried:

_Props[tmpObject.Key] = Convert.ChangeType(rst.Fields[TmpString].Value,tmpObject.Value.GetType());
Posted
Comments
Sascha Lefèvre 17-Feb-16 9:14am    
Exactly which kind of type conversion fails (input type, desired output type)?
Member 11736914 17-Feb-16 9:17am    
output type
Sascha Lefèvre 17-Feb-16 9:24am    
No, I mean: Attempting to convert from type A to type B fails. What is A and what is B?
Member 11736914 17-Feb-16 10:20am    
it basically should work for any type cause it's defined in Convert Class
Sascha Lefèvre 17-Feb-16 10:26am    
Please run your project. And when you hit that error, hover over "Value" of "rst.Fields[TmpString].Value" and over "Value" of "tmpObject.Value" and observe the type information in the popup. Then please tell me those type names.

1 solution

i solved it , the problem was in the itterator var tmpObject it can't adapt to other types
so if my first element was an integer the var remine an integer so i used a different way to access informations that exists in my dictionary
C#
for (int i = 0; i < _Props.Count; i++)
           {
               String TmpString = GetKey(_Props.Keys.ElementAt(i), BdInfo);
               _Props[_Props.Keys.ElementAt(i)] = Convert.ChangeType(rst.Fields[TmpString].Value, _Props.Keys.ElementAt(i).GetType());

           }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900