gpt4 book ai didi

C# 部分类和 get 访问器

转载 作者:行者123 更新时间:2023-12-02 22:06:16 25 4
gpt4 key购买 nike

基于 Entity Framework 实体,我为其中之一创建了一个分部类。问题是,我想在返回 ContentValue 之前添加一些代码。但它不起作用 -

[MetadataType(typeof(ToolTip_Meta))]
public partial class Tooltip
{
..some methods etc
}

public class ToolTip_Meta
{
[Required]
public string ContentValue
{
get
{
if (!string.IsNullOrEmpty(this.ContentValue))
return this.ContentValue.Replace("\n", "<br/>").Replace("\r", "").Replace("\r", "").Replace("'", "\\'").Replace("\"", "&quot;");
return this.ContentValue;
}
set { ContentValue = value; }
}
}

当我在项目的某处想要获取 ContentValue 属性值时,我得到的是未更改的字符串,但我没有进入 get 访问器。如何解决?

最佳答案

我认为这实际上与部分类或元数据没有任何关系。你的问题其实很简单。

if (!string.IsNullOrEmpty(this.ContentValue))

当您访问 this.ContentValue 时,它会调用该 getter,然后该 getter 会访问 this.ContentValue,后者会调用该 getter...因此您要么获取 StackOverflowException 或 OutOfMemoryException。

如果您没有遇到这两个错误之一,请更新您的问题以解释它如何不起作用。

关于C# 部分类和 get 访问器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16067120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com