开发者

Exception in property when setting a binary value

I have the following method which gives me a stackoverflow exception on setting a value. I can't step into it and debug it application just crashes few seconds later. Here is the code

public byte[] ImageTIFF
{
    get
    {
        try
        {
            string filePath = ImageTIFFDir + ImageId + ".TIFF";

            if (File.Exists(filePath))
            {
                return File.ReadAllBytes(ImageTIFFDir + ImageCollectionId + ".TIFF");
     开发者_StackOverflow社区       }
            else
            {
                return null;        
            }
        }
        catch (Exception e)
        {
            Logger.log("Can't read TIFF image from file.", e);
            throw;
        }
    }
    set
    {
        try
        {
            ImageTIFF = value;
        }
        catch (Exception e)
        {
            Logger.log("Can't set image TIFF.", e);
            throw;
        }

    }
}

This gets called during an export and it fails on a first call:

someImage.ImageTIFF = File.ReadAllBytes(imageName);


Change the code to

private byte[] m_ImageTIFF;
public byte[] ImageTIFF 
{
  get{...}
  set { m_ImageTIFF = value;}
}

You need a backing store for the property.


This is because , you are setting property itself in the set block

ImageTIFF = value;

make a byte[] array variable and set in the set block and in the get block return this variable.


You're setting the value of Property within setting the value of the property...recursion, my friend:

ImageTIFF = value;

You'll need to create another variable to hole value. Properties are no variables, they're just wrappers.


Here you try to set value to the property you are already in.

try
{
    ImageTIFF = value;
}

You need to have class member behind that property. Something like

byte[] _imageTIFF

and set it

set
{
    try
    {
        _imageTIFF = value;
    }
    catch (Exception e)
    {
        Logger.log("Can't set image TIFF.", e);
        throw;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜