Re: Getting a byte array from a generic class

Steve Johnson <[email protected]> Tue, 18 May 2010 13:05:08 -0400
Newsgroups gmane.comp.windows.devel.dotnet.advanced
Message-ID <[email protected]>
That's actually a great use of a dynamic.  Cool.

--
Steve Johnson

On Tue, May 18, 2010 at 12:55 PM, Peter Ritchie <
[email protected]> wrote:

> Rather than the bad smell of a type comparison, I prefer to use a more
> object-oriented approach with polymorphism (overloading in this example).
>  But, since we're dealing with a generic class that's trying to use a
> non-generic method; this get's a little tricky.
>
> If you're using .NET 4.0, you can create a dynamic variable of the value
> and let the runtime deal with finding the overload.  For example:
>         public abstract class NumericNtcipObject<T> where T : struct
>         {
>                private readonly T value;
>
>                protected NumericNtcipObject(T value)
>                {
>                        this.value = value;
>                }
>
>                private static byte[] ToArray(int v)
>                {
>                        return BitConverter.GetBytes(v);
>                }
>
>                private static byte[] ToArray(uint v)
>                {
>                        return BitConverter.GetBytes(v);
>                }
>                //...
>                public byte[] ValueAsByteStream()
>                {
>                        dynamic temp = value;
>                        try
>                        {
>                                return ToArray(temp);
>                        }
>                        catch
> (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException)
>                        {
>                                return null;
>                        }
>                }
>        }
>
> If you're not using .NET 4.0 (and using .NET 3.x) then you can use
> reflection to do the same thing that the dynamic runtime would do (which is
> the visitor pattern or double-dispatch):
>
>        public abstract class NumericNtcipObject<T> where T : struct
>         {
>                private readonly T value;
>
>                protected NumericNtcipObject(T value)
>                {
>                        this.value = value;
>                }
>
>                protected static byte[] ToArray(int v)
>                {
>                        return BitConverter.GetBytes(v);
>                }
>
>                protected static byte[] ToArray(uint v)
>                {
>                        return BitConverter.GetBytes(v);
>                }
>                //...
>
>                public byte[] ValueAsByteStream()
>                {
>                        var method = from m in
> GetType().GetMethods(BindingFlags.NonPublic | BindingFlags.Static |
> BindingFlags.FlattenHierarchy)
>                                                 where m.Name == "ToArray"
>                                                           &&amp;
> m.GetParameters().Length == 1
>                                                           &&amp;
> value.GetType().IsAssignableFrom
>
>  (m.GetParameters()[0].ParameterType)
>                                                           &&amp;
> m.ReturnType == typeof(byte[])
>                                                 select m;
>
>                        return (byte[])method.Single().Invoke(this, new
> object[] { value });
>                }
>        }
>
> If you're prior to .NET 3.0, convert the LINQ statement to something that
> will work in .NET 2.0.
>
> ===================================
> View archives and manage your subscription(s) at
> http://peach.ease.lsoft.com/archives
>

===================================
View archives and manage your subscription(s) at http://peach.ease.lsoft.com/archives