【问题标题】:How to run a string command that is a using decorator pattern [duplicate]如何运行使用装饰器模式的字符串命令[重复]
【发布时间】:2016-04-09 03:23:36
【问题描述】:

我有一段代码产生以下输出

新导师(新讲师(新课程协调员()))

我需要一种方法来允许 c# 将其作为字符串执行,但是在搜索后我无法找到任何与字符串相关的示例,例如这些。任何有关解决此问题的最佳方法的想法都将不胜感激。

【问题讨论】:

标签: c# decorator


【解决方案1】:

如果我理解正确,那么您应该可以通过 implicit 运算符重载来做到这一点。 Here it is on MSDN

//To use, put in the "tutor" class.
public static implicit operator string(tutor t)
{
    //return the tutor object, in string form.
    //For the example, I just return the .ToString() function found on all objects.
    return t.ToString();
}

这将让您无缝地使用语句“新导师(新讲师(新课程协调员()))”作为字符串。 编辑:我想我误解了,也许这个答案无论如何都会对路人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-06
    • 2021-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-07
    • 2014-02-04
    相关资源
    最近更新 更多