Solved

Is it possible to cast to generic 'object' type?

Posted on 2008-09-30
3
273 Views
Last Modified: 2013-12-17
Sorry for the C# beginner question but here goes...

say I have a declared type such as:
        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        public struct SAMPLE_STRUCT
        {
            public UInt32 size;
            public Boolean boolVar;
            public Int32 intVar;
        };

and have instances of it as in

            SAMPLE_STRUCT ss1 = new SAMPLE_STRUCT();
            SAMPLE_STRUCT ss2 = new SAMPLE_STRUCT();

and I want to send it to a class that have generic methods such as

            byte[] ss1Array = sCopy.StructureToByteArray(ss1);
            sCopy.ByteArrayToStructure(ss1Array, ref ss2);

where the interface is defined as

                public byte[] StructureToByteArray(object obj)
                { ... }

                public void ByteArrayToStructure(byte[] byteArray, ref object obj)
                { ... }

How do I get around the following errors:

The best overloaded method match for 'BD.EpiCenter.Utility.clsStructCopy.ByteArrayToStructure(byte[], ref object)' has some invalid arguments      
Argument '2': cannot convert from 'ref TestStructCopy.Program.SAMPLE_STRUCT' to 'ref object'

Is there a way to cast a typed variable to an obect/ref object such as
           sCopy.ByteArrayToStructure(ss1Array, ref (object) ss2);

which obviously does not work or must I introduce a temporary object to pass into the method and copy out of after it completes as below which does work?

           object ss2Obj = ss2;
            sCopy.ByteArrayToStructure(ss1Array, ref ss2);
            ss2 = (SAMPLE_STRUCT) ss2Obj;
0
Comment
Question by:mfenske17363
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
johnaryan earned 125 total points
ID: 22606688
What you're referring to is known as "boxing" in c#

Here's a reference to an example
http://www.java2s.com/Tutorial/CSharp/0120__struct/Boxandunboxastruct.htm
0
 

Author Comment

by:mfenske17363
ID: 22607281
So there is no way to cast down a type to a base 'object' and that I would need to create a new generic object instance and manipulate/pass it where a parameter of type 'object' is needed?

0
 
LVL 5

Expert Comment

by:johnaryan
ID: 22609134
Not if it's a value type (like a struct), you could have created it as a class instead and then you could have cast it to an object type.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now