Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Say we have the following (simplified version part of a bigger code base to show the problem):
public class someclass<T>
{
   public void somemethod(object obj)
   {
       T o = (T)obj;
   }
}
And we call the class as follows :
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 15-Apr-12 0:05am
Mehdi Gholam244.2K
Comments
Nelek at 15-Apr-12 7:06am
   
Interesting question. +5
Mehdi Gholam at 15-Apr-12 7:31am
   
Came across this while debugging RaptorDB the doc version.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Found a solution as follows :
...
   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;
   }
  Permalink  
Comments
VJ Reddy at 15-Apr-12 7:01am
   
Good answer. 5!
Mehdi Gholam at 15-Apr-12 7:34am
   
Thanks VJ!
Espen Harlinn at 15-Apr-12 18:39pm
   
5'ed!
Mehdi Gholam at 16-Apr-12 0:52am
   
Thanks
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Boxing - always fun with generics!
Try this:
 
        public class someclass<T>
            {
            public void somemethod(object obj)
                {
                T o = (T) Convert.ChangeType(obj, typeof(T));
                }
            }
  Permalink  
Comments
Nelek at 15-Apr-12 7:06am
   
Elegant way. +5
Mehdi Gholam at 15-Apr-12 7:10am
   
5'ed, I will have to check the performance difference of always converting vs checking the types beforehand.
OriginalGriff at 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 at 15-Apr-12 7:28am
   
Wow! you saved me the trouble, cheers :)
OriginalGriff at 15-Apr-12 7:36am
   
Welcome!
 
someclass s = new someclass();
Stopwatch s1 = new Stopwatch();
s1.Start();
int v1 = 1;
for (int i = 0; i < 10000; i++)
{
s.somemethod(v1);
}
s1.Stop();
someclass2 t = new someclass2();
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
{
public void somemethod(object obj)
{
T o = (T)Convert.ChangeType(obj, typeof(T));
}
}
public class someclass2
{
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
Espen Harlinn at 15-Apr-12 18:39pm
   
5'ed!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

someclass<byte> s = new someclass<byte>();
 
   s.somemethod(1);
 

 
conversion of Int to byte not feasible, due to size.
 
Explicit casting may work
  Permalink  
v2
Comments
Mehdi Gholam at 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)



Advertise | Privacy | Mobile
Web04 | 2.8.141022.1 | Last Updated 15 Apr 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100