Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Problems serializing class with float[,] property

Posted on 2008-06-14
1
Medium Priority
?
955 Views
Last Modified: 2013-11-15
I am receiving a reflection error will serializing a class that is the body of MSMQ body. Messages is "There was an error reflecting type 'SSPW.ThermalMonitor.Interfaces.RawImage'." I have many other classes going out over the same queue without problems. The serialization error is occuring on the Pixels property is a 2D float array (float[,]). I have verified this by adding the XmlIgnoreAttribute()] to the Pixels property. In this case the message is serialized and sent successfully.

What do i have to do to serialize a simple typed 2D array? My message body class in given below.


using System;
using System.Xml.Serialization;
 
namespace SSPW.ThermalMonitor.Interfaces
{
    [Serializable]
    public class RawImage
    {
        protected string _handle;
        protected int _width;
        protected int _height;
        protected float[,] _pixels;
        protected DateTime _timestamp;
        
        public RawImage()
        {
            _handle = "";
            _width = 320;
            _height = 240;
            _pixels = new float [_width,_height];
            _timestamp = DateTime.Now;
        }
 
        public String Handle
        {
            get
            {
                return _handle;
            }
            set
            {
                _handle = value;
            }
        }
 
        public int Width
        {
            get
            {
                return _width;
            }
            set
            {
                _width = value;
            }
        }
 
        public int Height
        {
            get
            {
                return _height;
            }
            set
            {
                _height = value;
            }
        }
 
        //[XmlIgnoreAttribute()]
        public float[,] Pixels
        {
            get
            {
                return _pixels;
            }
            set
            {
                _pixels = value;
            }
        }
 
        public DateTime Timestamp
        {
            get
            {
                return _timestamp;
            }
            set
            {
                _timestamp = value;
            }
        }
 
    }
}

Open in new window

0
Comment
Question by:cravenjw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 1

Accepted Solution

by:
gregmulvihill earned 1000 total points
ID: 21787410
Give this a try.
using System;
using System.Xml.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
 
namespace SSPW.ThermalMonitor.Interfaces
{
	[Serializable]
	public class RawImage
	{
		protected string _handle;
		protected int _width;
		protected int _height;
		protected float[,] _pixels;
		protected DateTime _timestamp;
 
		public RawImage()
		{
			_handle = "";
			_width = 320;
			_height = 240;
			_pixels = new float[_width, _height];
			_timestamp = DateTime.Now;
		}
 
		public String Handle
		{
			get { return _handle; }
			set { _handle = value; }
		}
 
		public int Width
		{
			get { return _width; }
			set { _width = value; }
		}
 
		public int Height
		{
			get { return _height; }
			set { _height = value; }
		}
 
		[XmlIgnoreAttribute()]
		public float[,] Pixels
		{
			get { return _pixels; }
			set { _pixels = value; }
		}
 
		[XmlElementAttribute("Pixels")]
		public byte[] PixelsByteArray
		{
			get
			{
				if (_pixels != null)
				{
					MemoryStream oMemoryStream = new MemoryStream();
 
					BinaryFormatter oBinaryFormatter = new BinaryFormatter();
					oBinaryFormatter.Serialize(oMemoryStream, _pixels);
 
					byte[] oBytes = oMemoryStream.ToArray();
 
					oMemoryStream.Close();
					oMemoryStream.Dispose();
 
					return oBytes;
				}
				else
				{
					return null;
				}
			}
 
			set
			{
				if (value != null)
				{
					MemoryStream oMemoryStream = new MemoryStream(value);
 
					BinaryFormatter oBinaryFormatter = new BinaryFormatter();
					_pixels = oBinaryFormatter.Deserialize(oMemoryStream) as float[,];
 
					oMemoryStream.Close();
					oMemoryStream.Dispose();
				}
				else
				{
					_pixels = null;
				}
			}
		}
 
		public DateTime Timestamp
		{
			get { return _timestamp; }
			set { _timestamp = value; }
		}
	}
}

Open in new window

0

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Want to know how to use Exchange Server Eseutil command? Go through this article as it gives you the know-how.
Here in this article, you will get a step by step guidance on how to restore an Exchange database to a recovery database. Get a brief on Recovery Database and how it can be used to restore Exchange database in this section!
Viewers will learn how to use the Hootsuite Dashboard.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

604 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