Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Say we have the following (simplified version part of a bigger code base to show the problem):
C#
public class someclass<T>
{
   public void somemethod(object obj)
   {
       T o = (T)obj;
   }
}

And we call the class as follows :
C#
public void main()
{
   someclass<byte> s = new someclass<byte>();
   
   s.somemethod(1); // passing an int (the compiler should be passing a byte but it's not)
}

We get an invalid cast error. How can this be resolved.
Posted
Comments
Nelek 15-Apr-12 7:06am    
Interesting question. +5
Mehdi Gholam 15-Apr-12 7:31am    
Came across this while debugging RaptorDB the doc version.

Found a solution as follows :
C#
...
   public void somemethod(object obj)
   {
            T o = default(T);
            if(typeof(T).Equals(obj.GetType()) == false )
            {
                o = (T) Convert.ChangeType(obj, typeof(T));
            }
            else
                o = (T)obj;
   }
 
Share this answer
 
Comments
VJ Reddy 15-Apr-12 7:01am    
Good answer. 5!
Mehdi Gholam 15-Apr-12 7:34am    
Thanks VJ!
Espen Harlinn 15-Apr-12 18:39pm    
5'ed!
Mehdi Gholam 16-Apr-12 0:52am    
Thanks
Boxing - always fun with generics!
Try this:

C#
public class someclass<T>
    {
    public void somemethod(object obj)
        {
        T o = (T) Convert.ChangeType(obj, typeof(T));
        }
    }
 
Share this answer
 
Comments
Nelek 15-Apr-12 7:06am    
Elegant way. +5
Mehdi Gholam 15-Apr-12 7:10am    
5'ed, I will have to check the performance difference of always converting vs checking the types beforehand.
OriginalGriff 15-Apr-12 7:22am    
:laugh: - There were no answers when I started!
I'm only guessing but I suspect that since ChangeType is just a wrapper for the IConvertible method call, that it will be quicker to always convert - the type-to-same-type method is normally no-operation.
So I checked, and I'm way, way wrong: to check first is about twice as fast as not checking! 5 to you!
Mehdi Gholam 15-Apr-12 7:28am    
Wow! you saved me the trouble, cheers :)
OriginalGriff 15-Apr-12 7:36am    
Welcome!

someclass<byte> s = new someclass<byte>();
Stopwatch s1 = new Stopwatch();
s1.Start();
int v1 = 1;
for (int i = 0; i < 10000; i++)
{
s.somemethod(v1);
}
s1.Stop();
someclass2<byte> t = new someclass2<byte>();
byte v2 = 1;
Stopwatch s2 = new Stopwatch();
s2.Start();
for (int i = 0; i < 10000; i++)
{
t.somemethod(v2);
}
s2.Stop();
Console.WriteLine("{0}:{1}", s1.ElapsedTicks, s2.ElapsedTicks);
}

public class someclass<t>
{
public void somemethod(object obj)
{
T o = (T)Convert.ChangeType(obj, typeof(T));
}
}
public class someclass2<t>
{
public void somemethod(object obj)
{
T o = default(T);
if (typeof(T).Equals(obj.GetType()) == false)
{
o = (T)Convert.ChangeType(obj, typeof(T));
}
else
o = (T)obj;
}
Results: 7800:4160
XML
someclass<byte> s = new someclass<byte>();

   s.somemethod(1);




conversion of Int to byte not feasible, due to size.

Explicit casting may work
 
Share this answer
 
v2
Comments
Mehdi Gholam 15-Apr-12 6:54am    
This is not an 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