问题:

我有一种情况,我想使用方法组语法而不是匿名方法(或lambda语法)来调用函数。

该函数有两个重载,一个重载执行Action ,另一个重载Func<string>

我可以使用匿名方法(或lambda语法)愉快地调用这两个重载,但是如果我使用方法组语法,则会出现歧义调用的编译器错误。 我可以通过显式强制转换为ActionFunc<string> ,但不要认为这是必需的。

谁能解释为什么需要显式强制转换。

下面的代码示例。

class Program
{
    static void Main(string[] args)
    {
        ClassWithSimpleMethods classWithSimpleMethods = new ClassWithSimpleMethods();
        ClassWithDelegateMethods classWithDelegateMethods = new ClassWithDelegateMethods();

        // These both compile (lambda syntax)
        classWithDelegateMethods.Method(() => classWithSimpleMethods.GetString());
        classWithDelegateMethods.Method(() => classWithSimpleMethods.DoNothing());

        // These also compile (method group with explicit cast)
        classWithDelegateMethods.Method((Func<string>)classWithSimpleMethods.GetString);
        classWithDelegateMethods.Method((Action)classWithSimpleMethods.DoNothing);

        // These both error with "Ambiguous invocation" (method group)
        classWithDelegateMethods.Method(classWithSimpleMethods.GetString);
        classWithDelegateMethods.Method(classWithSimpleMethods.DoNothing);
    }
}

class ClassWithDelegateMethods
{
    public void Method(Func<string> func) { /* do something */ }
    public void Method(Action action) { /* do something */ }
}

class ClassWithSimpleMethods
{
    public string GetString() { return ""; }
    public void DoNothing() { }
}

C#7.3更新

根据0xcde在2019年3月20日(我发布此问题的九年后!)的评论,由于改进了重载候选 ,此代码从C#7.3开始编译。

I have a scenario where I want to use method group syntax rather than anonymous methods (or lambda syntax) for calling a function.The function has two overloads, one that takes an Action , the other takes a Func<string> .I can happily call the two overloads using anonymous methods (or lambda syntax), but get a compiler error of Ambiguous invocation if I use method group syntax.I can workaround by explicit casting to Action or Func<string> , but don’t think this should be necessary.Can anyone explain why the explicit casts should be required.Code sample below.C# 7.3 UpdateAs per 0xcde ‘s comment below on March 20 2019 (nine years after I posted this question!), this code compiles as of C# 7.3 thanks to improved overload candidates .

问题:

我在尝试启动应用程序时收到此错误消息。

尝试确定托管应用程序的DNX进程的进程ID时发生错误

有没有办法解决这个问题?

I get this error message when I’m trying to start the application.An error occurred attempting to determine the process id of the DNX process hosting your applicationIs there a way to fix the problem?

问题:

当我调用XmlNode.AppendChild() ,出现此错误:

要插入的节点来自其他文档上下文。

static public XmlNode XMLNewChildNode(XmlNode oParent, string sName, 
    string sNamespaceURI, string sNodeType)
{
    XmlNode oNode = moDoc.CreateNode(sNodeType, sName, sNamespaceURI);
    oParent.AppendChild(oNode);
    return oNode;
}

这段代码是从它的VB 6.0版本转换而来的(请忽略可选参数,在C#版本中有重载):

Public Function XMLNewChildNode(ByVal oParent As IXMLDOMNode, ByVal _
    sName As String, Optional ByVal sNamespaceURI As String = "", _
    Optional ByVal sNodeType As String = "element") As IXMLDOMNode
'**************** DESCRIPTION *******************
  'Create a new Child Node for passed Parent.
'***************** VARIABLES ********************
  Dim oNode As IXMLDOMNode
'************************************************
  Set oNode = moDoc.createNode(sNodeType, sName, sNamespaceURI)
  Call oParent.appendChild(oNode)
  Set XMLNewChildNode = oNode
End Function

为什么VB代码可以工作而C#不能工作? 我需要知道,VB和C#处理XML的方式之间有区别吗?

When I am calling XmlNode.AppendChild() , I get this error:The node to be inserted is from a different document context.This code was converted from its VB 6.0 version which was this (please ignore the optional parameters, I have overloads for them in C# version):Why does the VB code work while the C# does not?Are there differences between how VB and C# handle XML, that I need to be aware of?

问题:

我有一些基本代码来确定我的MVC应用程序中的错误。 当前,在我的项目中,我有一个名为Error的控制器,该控制器具有操作方法HTTPError404() , HTTPError500()General() 它们都接受字符串参数error 使用或修改下面的代码。 将数据传递给错误控制器进行处理的最佳/正确方法是什么? 我想有一个尽可能强大的解决方案。

protected void Application_Error(object sender, EventArgs e)
{
    Exception exception = Server.GetLastError();
    Response.Clear();

    HttpException httpException = exception as HttpException;
    if (httpException != null)
    {
        RouteData routeData = new RouteData();
        routeData.Values.Add("controller", "Error");
        switch (httpException.GetHttpCode())
        {
            case 404:
                // page not found
                routeData.Values.Add("action", "HttpError404");
                break;
            case 500:
                // server error
                routeData.Values.Add("action", "HttpError500");
                break;
            default:
                routeData.Values.Add("action", "General");
                break;
        }
        routeData.Values.Add("error", exception);
        // clear error on server
        Server.ClearError();

        // at this point how to properly pass route data to error controller?
    }
}

I have some basic code to determine errors in my MVC application.Currently in my project I have a controller called Error with action methods HTTPError404() , HTTPError500() , and General() .They all accept a string parameter error .Using or modifying the code below.What is the best/proper way to pass the data to the Error controller for processing?I would like to have a solution as robust as possible.

问题:

我创建了一个程序,试图在网站上发布一个字符串,我收到此错误:

“服务器提交了协议违规.Section = ResponseStatusLine”

在这行代码之后:

gResponse = (HttpWebResponse)gRequest.GetResponse(); 

我该如何解决这个异常?

I have created a program, tried to post a string on a site and I get this error:”The server committed a protocol violation. Section=ResponseStatusLine”after this line of code:How can I fix this exception?