我在服务器端有一个结构,布局如下:
struct SomeStruct
{
    public string SomeString { get; set; };
    public string SomeString1;
    public string SomeString2;
    public string SomeString3;
}
我正在使用客户端/服务器模型,并且该结构的一个实例被引用了很多次,因为它具有非常重要的信息(超过 200 次)。
问题是当某个函数被调用时,这个结构中的值变为空。我不知道为什么,它一直困扰着我很长时间。
在意识到这个值为空之前,我调用了很多方法,所以我不知道我的代码的哪一部分使我的字符串无效。
我正在使用 VS2012,但我也有 2010 和 2008 终极版。我想知道当某些代码部分使我的字符串无效时是否有一种方法可以执行触发器。
我尝试添加一些这样的属性,但从未抛出异常:
struct SomeStruct {
    string somestr;
    public string SomeString
    {
        get { return somestr; }
        set
        {
            if (value == null)
            {
                throw new Exception("stirng is null");
            }
            somestr = value;
        }
    }
    public string SomeString1;
    public string SomeString2;
    public string SomeString3;
}
可能不重要,但这是我正在使用的结构之一(Name 变量在我的代码的某些部分变为 null,其余变为 default()):
[ProtoContract]
public struct CharacterInformation
{
    [ProtoMember(2)]
    public string Name;
    [ProtoMember(3)]
    public IntegerVector2 Position;
    [ProtoMember(5)]
    public CharacterDirection Direction;
    [ProtoMember(6)]
    public CharacterStatus Status;
    [ProtoMember(7)]
    public CharacterClass Class;
    [ProtoMember(8)]
    public CharacterRace Race;
    [ProtoMember(9)]
    public CharacterType Type;
    [ProtoMember(10)]
    public CharacterFaction Faction;
    [ProtoMember(11)]
    public float MovementModifier;
    [ProtoMember(12)]
    public CharacterEquipment Equipment;
}
编辑:此结构的唯一实例是在与 Sql 相关的函数上创建的:
public CharacterServerInformation GetInformation(int charID)
        {
            CharacterServerInformation information = new CharacterServerInformation();
            if (!authInstance.CharacterExists(charID))
            {
                // char doesn't exists
                throw new Exception("Character doesn't exists");
            }
            information.ID = charID;
            information.Experience = GetExperience(charID);
            information.Info.Direction = CharacterDirection.Bottom;
            information.Info.Name = authInstance.CharacterGetName(charID);
            information.Info.Class = GetClass(charID);
            information.Info.Faction = GetFaction(charID);
            information.Info.Position = GetPosition(charID);
            information.Info.Race = GetRace(charID);
            information.Info.Status = GetStatus(charID);
            information.Info.Type = GetType(charID);
            information.Info.MovementModifier = 1f; // should store old movement modifier, but well, whatever
            information.HealthLeft = GetHealthLastLogout(charID);
            return information;
        }