Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我怎么能在c中做到这一点#_C#_Frameworks - Fatal编程技术网

C# 我怎么能在c中做到这一点#

C# 我怎么能在c中做到这一点#,c#,frameworks,C#,Frameworks,我想制作一个框架,它的函数调用风格不同于c#风格 像实例一样创建 Documment doc= new Document("required param is here"); doc("otherinfo").Dothis(); dot此函数基于用户在创建新实例时传递的信息和用户在创建新实例时传递的其他信息进行调用 它有点像jQuery。比如$(“#goo”)。长度 在c#中可以这样做吗 //class called document public class document

我想制作一个框架,它的函数调用风格不同于c#风格

像实例一样创建

Documment doc= new Document("required param is here");

doc("otherinfo").Dothis(); 
dot此函数基于用户在创建新实例时传递的信息和用户在创建新实例时传递的其他信息进行调用

它有点像jQuery。比如
$(“#goo”)。长度

在c#

中可以这样做吗

//class called document
    public class document
    {
    //constructor for the class
    public document(string s)
    {

    }

    public void dothis()
    {
    //dothis method.
    }
    }

Try that?


您需要调用doc的函数,doc(“second”)是无效的,按照doc.get(“second”).dothis()的顺序;可能是有效的,这取决于“document”对象中可用的函数。

您需要调用doc的函数,doc(“second”)是无效的,按照doc.get(“second”).dothis()的顺序;可能是有效的,这取决于“document”对象中可用的函数。

我不认为没有对C#进行一些语法更改,您所要求的是可能的。

我不认为没有对C#进行一些语法更改,您所要求的是可能的。

我认为扩展方法在这种情况下是可能的,但不是很确定

我认为扩展方法在这种情况下是可能的,但不是很确定

听起来你好像想要咖喱,但对于一门课来说:

private Func<string,Document> CurryNewDocument(string first)
{
    return second => new Document(first, second);
}


var factory = CurryNewDocument("required param is here");

Document doc = factory("other info");
doc.DoThis();
private Func CurryNewDocument(字符串优先)
{
返回第二个=>新文档(第一个,第二个);
}
var factory=CurryNewDocument(“此处为所需参数”);
文件文件=工厂(“其他信息”);
DoThis()博士;

如果创造不需要以这种方式发生,请仔细研究。委托允许您将与特定方法签名匹配的方法分配给变量。事实上,上面的
Func
是一个通用委托。

这听起来像是你想要咖喱,但对于一个类:

private Func<string,Document> CurryNewDocument(string first)
{
    return second => new Document(first, second);
}


var factory = CurryNewDocument("required param is here");

Document doc = factory("other info");
doc.DoThis();
private Func CurryNewDocument(字符串优先)
{
返回第二个=>新文档(第一个,第二个);
}
var factory=CurryNewDocument(“此处为所需参数”);
文件文件=工厂(“其他信息”);
DoThis()博士;

如果创造不需要以这种方式发生,请仔细研究。委托允许您将与特定方法签名匹配的方法分配给变量。事实上,上面的
Func
是一个通用的委托。

我不相信你能得到确切的语法,但你可以非常接近:

Document doc = Document.Generator("required parameter");
doc("params").DoThis();
文档必须是委托类型,返回封装参数的帮助器类型

Document doc = new Document("required parameter");
doc["params"].DoThis();

Document是一个普通的类,带有返回帮助器类型的默认索引器。

我不相信您可以得到确切的语法,但您可以非常接近:

Document doc = Document.Generator("required parameter");
doc("params").DoThis();
文档必须是委托类型,返回封装参数的帮助器类型

Document doc = new Document("required parameter");
doc["params"].DoThis();

Document是一个普通类,带有返回帮助器类型的默认索引器。

也许您正试图通过它的框架实现JavaScript提供的功能,但使用C#,您将无法实现这一点

即使当前的C#实现允许这样做,在区分什么是函数调用和什么是对象重新实例化时也会存在某种模糊性

doc("second") 文件(“第二份”)
可能您正试图通过JavaScript的框架实现它所提供的功能,但是使用C#,您将无法做到这一点

即使当前的C#实现允许这样做,在区分什么是函数调用和什么是对象重新实例化时也会存在某种模糊性

doc("second") 文件(“第二份”) 将是不明确的。

在C#中,indexer具有
obj[args]
语法,因此您可以编写如下内容:

public SomeTypeOfYours this[string query]
{ get { return ...(implementation)... } }
然后调用者可以访问
doc[“whatever”].SomeMethod(…)
,其中
SomeMethod
是在您的
SomeTypeOfYours
上定义的。
newdocument(“abc”)
的字符串只是一个构造函数参数,因此:

public Document(string paramName) {...}
在C#中,indexer具有
obj[args]
语法,因此您可以编写如下内容:

public SomeTypeOfYours this[string query]
{ get { return ...(implementation)... } }
然后调用者可以访问
doc[“whatever”].SomeMethod(…)
,其中
SomeMethod
是在您的
SomeTypeOfYours
上定义的。
newdocument(“abc”)
的字符串只是一个构造函数参数,因此:

public Document(string paramName) {...}

doc[“otherinfo”].DoThis()可接受?将
doc[“otherinfo”].DoThis()可以接受吗?给我看代码,然后我就可以做了that@Ian:哪一个?但是现在很晚了,我要睡觉了。也许有人会帮你,如果没有的话,我会在早上帮你。我想你晚上有月光,但我的国家有阳光。好的,谢谢你的帮助。@Ian:看看Marc Gravell的答案,他进一步探讨了索引器的概念。给我看代码,然后我就可以做了that@Ian:哪一个?但是现在很晚了,我要睡觉了。也许有人会帮你,如果没有的话,我会在早上帮你。我想你晚上有月光,但我的国家有阳光。伊恩:看看马克·格雷威尔的答案,他进一步探讨了索引器的概念。