Converting byte array to generic type

V

vtjumper

I'm building a C# interface to an existing messaging system. The
messaging system allows values of several types to be sent/recieved
over the interface.

What I want to do is use a generic class to produce values in the
system. For instance I could create class

MsgGenericValue<UInt16>() which would represent an unsigned value on
the interface.

My issue is converting from byte [] values to the generic type T.

So if I have a class: MsgGenericValue<T>

With a member

T _Value

I want do be able to do something like

if (_Value is Int32) _Value =
BitConverter.ToInt32(Bytes, 0);

But it doesn't compile - It can't convert from Int32 to 'T'

So how do I do this? Any thoughts would be greatly appreciated

Tom
 
D

Dave Sexton

Hi Tom,

Having to check "_Value is Int32" is an indication that you shouldn't use
generics here. You should just have a regular property of type Object,
which will allow assignment from any type.
 
V

vtjumper

You're absolutely right, Dave.

However, if I do that, then I either have to:
- Write a bunch of specific classes for each type I want to support
or
- Always cast the return value from functions within the class

And I want to avoid that. I do have one way to do it - if I use an
object type variable within the routine, I can check the type; i.e.

T _Value;

object Temp = _Value; // temp to avoid casting
issues

if (_Value is Int32) Temp =
BitConverter.ToInt32(NewValue, 0);
else if (_Value is Boolean) Temp =
BitConverter.ToBoolean(NewValue, 0);
else if (_Value is Byte) Temp = NewValue[0];
else if (_Value is Double) Temp =
BitConverter.ToDouble(NewValue, 0);
else if (_Value is String) Temp =
BitConverter.ToString(NewValue);
else if (_Value is UInt32) Temp =
BitConverter.ToUInt32(NewValue, 0);
else if (_Value is UInt16) Temp =
BitConverter.ToUInt16(NewValue, 0);
else throw new InvalidTypeException("Unsupported type
" + _Value.GetType().ToString());

_Value = (T)Temp;

Kind of weird, but it works....

Thanks for the reply.

Tom


Hi Tom,

Having to check "_Value is Int32" is an indication that you shouldn't use
generics here. You should just have a regular property of type Object,
which will allow assignment from any type.

--
Dave Sextonhttp://davesexton.com/bloghttp://www.codeplex.com/DocProject(Sandcastle in VS IDE)

I'm building a C# interface to an existing messaging system. The
messaging system allows values of several types to be sent/recieved
over the interface.
What I want to do is use agenericclass to produce values in the
system. For instance I could create class
MsgGenericValue<UInt16>() which would represent an unsigned value on
the interface.
My issue is converting from byte [] values to thegenerictype T.
So if I have a class: MsgGenericValue<T>
With a member
I want do be able to do something like
if (_Value is Int32) _Value =
BitConverter.ToInt32(Bytes, 0);
But it doesn't compile - It can't convert from Int32 to 'T'
So how do I do this? Any thoughts would be greatly appreciated
 
D

Dave Sexton

Hi Tom,

Yes, that'll work :)

It's just that you're trading flexibility within the object for flexibility
outside of the object. In other words, externally you won't have to cast
the object, but if you ever need to support another type you'll have to
modify the object itself, meaning recompilation of the entire assembly just
to support another type. That means that assemblies that reference your
assembly may also require recompilation, even though the type is generic.

I don't know the business requirements for your application or how the
object will be used, obviously, but that's certainly something to consider.

--
Dave Sexton
http://davesexton.com/blog
http://www.codeplex.com/DocProject (Sandcastle in VS IDE)

You're absolutely right, Dave.

However, if I do that, then I either have to:
- Write a bunch of specific classes for each type I want to support
or
- Always cast the return value from functions within the class

And I want to avoid that. I do have one way to do it - if I use an
object type variable within the routine, I can check the type; i.e.

T _Value;

object Temp = _Value; // temp to avoid casting
issues

if (_Value is Int32) Temp =
BitConverter.ToInt32(NewValue, 0);
else if (_Value is Boolean) Temp =
BitConverter.ToBoolean(NewValue, 0);
else if (_Value is Byte) Temp = NewValue[0];
else if (_Value is Double) Temp =
BitConverter.ToDouble(NewValue, 0);
else if (_Value is String) Temp =
BitConverter.ToString(NewValue);
else if (_Value is UInt32) Temp =
BitConverter.ToUInt32(NewValue, 0);
else if (_Value is UInt16) Temp =
BitConverter.ToUInt16(NewValue, 0);
else throw new InvalidTypeException("Unsupported type
" + _Value.GetType().ToString());

_Value = (T)Temp;

Kind of weird, but it works....

Thanks for the reply.

Tom


Hi Tom,

Having to check "_Value is Int32" is an indication that you shouldn't use
generics here. You should just have a regular property of type Object,
which will allow assignment from any type.

--
Dave
Sextonhttp://davesexton.com/bloghttp://www.codeplex.com/DocProject(Sandcastle
in VS IDE)

I'm building a C# interface to an existing messaging system. The
messaging system allows values of several types to be sent/recieved
over the interface.
What I want to do is use agenericclass to produce values in the
system. For instance I could create class
MsgGenericValue<UInt16>() which would represent an unsigned value on
the interface.
My issue is converting from byte [] values to thegenerictype T.
So if I have a class: MsgGenericValue<T>
With a member
I want do be able to do something like
if (_Value is Int32) _Value =
BitConverter.ToInt32(Bytes, 0);
But it doesn't compile - It can't convert from Int32 to 'T'
So how do I do this? Any thoughts would be greatly appreciated
 
D

Dave Sexton

Hi,

Actually, the comments in my last post don't apply to your particular
situation since you're using BitConverter, which doesn't accept object - the
original problem :) Even if you used Object instead of a generic argument
you'd still have to implement the type checking as you have done. And
anyway, there is only a limited number of types that you have to check so
it's no big deal.

I should have looked at your code instead of jumping to conclusions.

--
Dave Sexton
http://davesexton.com/blog
http://www.codeplex.com/DocProject (Sandcastle in VS IDE)

You're absolutely right, Dave.

However, if I do that, then I either have to:
- Write a bunch of specific classes for each type I want to support
or
- Always cast the return value from functions within the class

And I want to avoid that. I do have one way to do it - if I use an
object type variable within the routine, I can check the type; i.e.

T _Value;

object Temp = _Value; // temp to avoid casting
issues

if (_Value is Int32) Temp =
BitConverter.ToInt32(NewValue, 0);
else if (_Value is Boolean) Temp =
BitConverter.ToBoolean(NewValue, 0);
else if (_Value is Byte) Temp = NewValue[0];
else if (_Value is Double) Temp =
BitConverter.ToDouble(NewValue, 0);
else if (_Value is String) Temp =
BitConverter.ToString(NewValue);
else if (_Value is UInt32) Temp =
BitConverter.ToUInt32(NewValue, 0);
else if (_Value is UInt16) Temp =
BitConverter.ToUInt16(NewValue, 0);
else throw new InvalidTypeException("Unsupported type
" + _Value.GetType().ToString());

_Value = (T)Temp;

Kind of weird, but it works....

Thanks for the reply.

Tom


Hi Tom,

Having to check "_Value is Int32" is an indication that you shouldn't use
generics here. You should just have a regular property of type Object,
which will allow assignment from any type.

--
Dave
Sextonhttp://davesexton.com/bloghttp://www.codeplex.com/DocProject(Sandcastle
in VS IDE)

I'm building a C# interface to an existing messaging system. The
messaging system allows values of several types to be sent/recieved
over the interface.
What I want to do is use agenericclass to produce values in the
system. For instance I could create class
MsgGenericValue<UInt16>() which would represent an unsigned value on
the interface.
My issue is converting from byte [] values to thegenerictype T.
So if I have a class: MsgGenericValue<T>
With a member
I want do be able to do something like
if (_Value is Int32) _Value =
BitConverter.ToInt32(Bytes, 0);
But it doesn't compile - It can't convert from Int32 to 'T'
So how do I do this? Any thoughts would be greatly appreciated
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top