将属性作为参数传递

发布于 2024-09-16 10:54:16 字数 1416 浏览 3 评论 0 原文

我想创建一个用于 LoadFromXML 加载和验证的通用辅助方法。如果我加载的 XML 不完整,我确实希望它完全失败而不抛出异常。目前,我的代码看起来像这样(或多或少)

public override bool Load(XElement source)
{
    return new List<Func<XElement, bool>>
    {
        i => this.LoadHelper(i.Element(User.XML_Username), ref this._username, User.Failure_Username),
        i => this.LoadHelper(i.Element(User.XML_Password), ref this._password, User.Failure_Password)
        //there are many more invokations of LoadHelper to justify this architecture
    }
    .AsParallel()
    .All(i => i.Invoke(source));
}

private bool LoadHelper(XElement k, ref string index, string failure)
{
    if (k != null && k.Value != failure)
    {
        index = k.Value;
        return true;
    }
    return false;
}

this._username 是属性 this.Username 使用的私有成员变量。这是我针对此问题的当前解决方案,但我面临一个主要问题:因为我无法将属性本身传递给 LoadHelper ,而 Action 则不能不匹配属性:(,我现在正在绕过属性设置器逻辑。

对于您自己的思考,在 LoadHelper 抽象之前,我的每个 List > 的条目看起来像这样...

i => ((Func<XElement, bool>)(k => { if (k == null || k.Value == User.Failure_Username) return false; 
                    { this.Username = k.Value; return true; } })).Invoke(i.Element(User.XML_Username)),

问题:有谁知道有什么方法可以在不绕过属性的 setter 逻辑的情况下做到这一点吗?

I want to create a generalized helper method for LoadFromXML loading and validation. If the XML I'm loading from is incomplete, I do want it to fail completely without throwing an exception. Currently, my code looks like this (more or less)

public override bool Load(XElement source)
{
    return new List<Func<XElement, bool>>
    {
        i => this.LoadHelper(i.Element(User.XML_Username), ref this._username, User.Failure_Username),
        i => this.LoadHelper(i.Element(User.XML_Password), ref this._password, User.Failure_Password)
        //there are many more invokations of LoadHelper to justify this architecture
    }
    .AsParallel()
    .All(i => i.Invoke(source));
}

private bool LoadHelper(XElement k, ref string index, string failure)
{
    if (k != null && k.Value != failure)
    {
        index = k.Value;
        return true;
    }
    return false;
}

this._username is a private member variable that is used by the property this.Username. This is the current solution I have for this problem, but I'm facing one major issue: Since I cannot pass the property itself to the LoadHelper and Action<string> doesn't match the property :(, I'm circumventing the property setter logic right now.

For your own musings, before the LoadHelper abstraction, each of my List<Func<XElement, bool>>'s entries looked like this...

i => ((Func<XElement, bool>)(k => { if (k == null || k.Value == User.Failure_Username) return false; 
                    { this.Username = k.Value; return true; } })).Invoke(i.Element(User.XML_Username)),

Question: Does anyone know any way to do this without circumventing the property's setter logic?

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

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

发布评论

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

评论(2

掩于岁月 2024-09-23 10:54:16

<块引用>

操作与属性不匹配

如果我没看错,您尝试用“Action”替换“ref string index”,然后尝试传递Protperty。接近但不完全是。怎么样?

private bool LoadHelper(XElement k, Action<string> setter, string failure) 
{ 
    if (k != null && k.Value != failure) 
    { 
        setter(k.Value); 
        return true; 
    } 
    return false; 
} 

然后

  i => this.LoadHelper(i.Element(User.XML_Username), s=>{this.Username = s},
                          User.Failure_Username),     

Action doesn't match the property

If I read that right, you tried replacing the "ref string index", with "Action<string>" and then tried passing the Protperty. Close but not quite. How 'bout?

private bool LoadHelper(XElement k, Action<string> setter, string failure) 
{ 
    if (k != null && k.Value != failure) 
    { 
        setter(k.Value); 
        return true; 
    } 
    return false; 
} 

then

  i => this.LoadHelper(i.Element(User.XML_Username), s=>{this.Username = s},
                          User.Failure_Username),     
哑剧 2024-09-23 10:54:16

我有时想知道 .Net 支持具有两个成员 Get 和 Set 的 iProperty(of T) 接口,并自动包装字段和属性,以便可以将 iProperty(of T) 参数传递给领域或财产。

使用匿名方法,可以通过创建一个 xProperty 类来创建这样一个不太可怕的东西,该类的构造函数采用获取和设置属性所需的方法。人们可以为任何希望其他类能够直接操作的属性定义该类的实例。不过,如果有一个标准接口,事情会好得多。不幸的是,我不知道有这样一个存在。

I've sometimes wondered how much it would bloat things for .Net to support an iProperty(of T) interface with two members, Get and Set, and automatically wrap fields and properties so that an iProperty(of T) parameter could be passed a field or property.

Using anonymous methods, one could create such a thing not too totally horribly by creating an xProperty class whose constructor took the methods necessary to get and set a property. One could define instances of the class for any properties that one wanted other classes to be able to manipulate directly. Things would be much nicer, though, if there were a standard interface. Unfortunately, I'm unaware of one existing.

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