检查结构的默认值

检查结构的默认值

本文介绍了检查结构的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也有一些是错误的,我默认值()函数。它总是返回,表示该结构不是默认值。



为什么会这样不行?



  [StructLayout(LayoutKind。顺序)] 
私人结构ArrayItem
{
众长SrcSize;

[的MarshalAs(UnmanagedType.ByValTStr,SizeConst = 250)]
公共字符串SRCFILE;

[的MarshalAs(UnmanagedType.ByValTStr,SizeConst = 250)]
公共字符串DestFile;
}

[StructLayout(LayoutKind.Sequential)]
私人结构MyInfo的
{
公众诠释计数;

公众诠释AppOne;

公众诠释AppTwo;

[的MarshalAs(UnmanagedType.ByValArray,SizeConst = 100,ArraySubType = UnmanagedType.Struct)]
公共ArrayItem []文件;
}


私人布尔默认值< T>(T构)
{
如果(EqualityComparer< T> .Default.Equals(结构,默认(T)))
返回真;
,否则
返回FALSE;
}

//成功返回'值改为如预期
MyInfo的fileInfoOne =新MyInfo的();
fileInfoOne.Count = 3;
fileInfoOne.Files =新ArrayItem [100];
fileInfoOne.Files [0] .SrcSize = 100;
Debug.Write(fileInfoOne:);
如果(默认值(fileInfoOne.Files [0]))的Debug.WriteLine(默认值);其他的Debug.WriteLine(数值改变。);


//失败,但所有默认设置,应该返回默认值
MyInfo的fileInfoTwo =新MyInfo的();
fileInfoTwo.Files =新ArrayItem [100];
fileInfoTwo.Files [0] .SrcSize = 0;
fileInfoTwo.Files [0] .SrcFile =;
fileInfoTwo.Files [0] .DestFile =;
Debug.Write(fileInfoTwo:);
如果(默认值(fileInfoTwo.Files [0]))的Debug.WriteLine(默认值);其他的Debug.WriteLine(数值改变。);


解决方案

不用担心,你的默认值()功能就好了:)



但是,当你调用它,确保你不会初始化测试结构成员空数组/字符串对象。 默认办法 0 (零)值类型和对于引用类型。 .NET框架阵列字符串是引用类型,所以,如果他们不是,该函数将其报告为非默认值。


There is something wrong in my DefaultValue() function. It ALWAYS returns false, representing that the the structure is NOT the default value.

Why would this not work?

[StructLayout(LayoutKind.Sequential)]
private struct ArrayItem
{
    public long SrcSize;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 250)]
    public string SrcFile;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 250)]
    public string DestFile;
}

[StructLayout(LayoutKind.Sequential)]
private struct MyInfo
{
    public int Count;

    public int AppOne;

    public int AppTwo;

    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 100, ArraySubType = UnmanagedType.Struct)]
    public ArrayItem[] Files;
}


private bool DefaultValue<T>(T structure)
{
    if (EqualityComparer<T>.Default.Equals(structure, default(T)))
        return true;
    else
        return false;
}

//Success returns 'Value Changed' as expected
MyInfo fileInfoOne = new MyInfo();
fileInfoOne.Count = 3;
fileInfoOne.Files = new ArrayItem[100];
fileInfoOne.Files[0].SrcSize = 100;
Debug.Write("fileInfoOne: ");
if (DefaultValue(fileInfoOne.Files[0])) Debug.WriteLine("Default Value."); else Debug.WriteLine("Value Changed.");


//Fails but has all the default settings, should return 'Default Value'
MyInfo fileInfoTwo = new MyInfo();
fileInfoTwo.Files = new ArrayItem[100];
fileInfoTwo.Files[0].SrcSize = 0;
fileInfoTwo.Files[0].SrcFile = "";
fileInfoTwo.Files[0].DestFile = "";
Debug.Write("fileInfoTwo: ");
if (DefaultValue(fileInfoTwo.Files[0])) Debug.WriteLine("Default Value."); else Debug.WriteLine("Value Changed.");
解决方案

No worry, your DefaultValue() function is just fine :)

But when you calling it, make sure you don't initialize the test struct members with empty array/string objects. default means 0(zero) for value types and null for reference types. NET Framework Arrays and Strings are reference types, so if they are not null, the function will report them as non default.

这篇关于检查结构的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 21:31