C# 对象引用应该自动更改映射字段

发布于 2024-12-10 23:11:25 字数 1135 浏览 0 评论 0原文

我尝试为我的 Entity Framework Code First 类编写一个 XML<>对象映射器。因为 EF 目前不支持这种自动 XML 序列化,所以我这样做是为了访问“Profile”对象属性并操作底层 ProfileData 字符串:

[Column(TypeName = "xml")]
public string ProfileData { get; set; }

[NotMapped]
public Profile Profile
{
    get { return ProfileData.DeserializeXml<Profile>(); }
    set { ProfileData = value.SerializeXml(); }
}

一切正常,但 ProfileData.DeserializeXml() 返回(当然)一个序列化的新对象来自 ProfileData 字符串。我认为扩展方法与我的问题无关,但我还是发布它:

public static T DeserializeXml<T>(this string xml) where T : class, new()
{
    if(string.IsNullOrWhiteSpace(xml)) return new T();
    var serializer = new XmlSerializer(typeof(T));
    using (var reader = new StringReader(xml))
    {
        try { return (T)serializer.Deserialize(reader); }
        catch { return null; } 
    }
}

当我想更改我的对象时,我这样做:

var profile = myObject.Profile;
profile.fooVar = "test";

但这不会影响我的 myObject,直到我这样做:

myObject.Profile = profile;

我知道为什么,但我不想为此,我想要...让我们用我的对象包装器“直接引用”底层 ProfileData 字符串。

有什么想法可以存档这个或者不可能吗?

I try to write a XML<>Object Mapper for my Entity Framework Code First class. Because EF doesn't support this automatic XML Serialization at the moment I did this to access the "Profile" object property and manipulate the underlying ProfileData string:

[Column(TypeName = "xml")]
public string ProfileData { get; set; }

[NotMapped]
public Profile Profile
{
    get { return ProfileData.DeserializeXml<Profile>(); }
    set { ProfileData = value.SerializeXml(); }
}

Everything works fine but ProfileData.DeserializeXml() returns (of course) a new object, serialized from the ProfileData string. I think the extension method isn't relevant for my question but I post it anyway:

public static T DeserializeXml<T>(this string xml) where T : class, new()
{
    if(string.IsNullOrWhiteSpace(xml)) return new T();
    var serializer = new XmlSerializer(typeof(T));
    using (var reader = new StringReader(xml))
    {
        try { return (T)serializer.Deserialize(reader); }
        catch { return null; } 
    }
}

When I want to change my object I do this:

var profile = myObject.Profile;
profile.fooVar = "test";

But this will not affect my myObject until I do this:

myObject.Profile = profile;

I know why but I don't want to do this, I want a... let us say "direct reference" to the underlying ProfileData string with my object wrapper.

Any idea to archive this or is not possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爺獨霸怡葒院 2024-12-17 23:11:25

为什么您的类不保留对 Profile 对象的引用,而仅在请求时进行序列化或反序列化?换句话说,将其更改为:

[Column(TypeName = "xml")]
public string ProfileData
{ 
    get { return Profile.SerializeXml(); }
    set { Profile = value.DeserializeXml<Profile>(); }
}

[NotMapped]
public Profile Profile { get; set; }

Why doesn't your class keep a reference to the Profile object instead, and only serialize or deserialize when requested? In other words, change it to:

[Column(TypeName = "xml")]
public string ProfileData
{ 
    get { return Profile.SerializeXml(); }
    set { Profile = value.DeserializeXml<Profile>(); }
}

[NotMapped]
public Profile Profile { get; set; }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文