C# 静态类型不能用作参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9595132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 08:03:00  来源:igfitidea点击:

C# Static types cannot be used as parameters

c#asp.nettypesstatic

提问by Tom Gullen

public static void SendEmail(String from, String To, String Subject, String HTML, String AttachmentPath = null, String AttachmentName = null, MediaTypeNames AttachmentType = null)
{
    ....

    // Add an attachment if required
    if (AttachmentPath != null)
    {
        var ct = new ContentType(MediaTypeNames.Text.Plain);
        using (var a = new Attachment(AttachmentPath, ct)
                    {
                        Name = AttachmentName,
                        NameEncoding = Encoding.UTF8,
                        TransferEncoding = TransferEncoding.Base64
                    })
        {
            mailMessage.Attachments.Add(a);
        }
    }

    ....
}

As you can see the MediaTypeNames AttachmentTypethrows the error:

如您所见,MediaTypeNames AttachmentType抛出错误:

'System.Net.Mime.MediaTypeNames': static types cannot be used as parameters

What is the best way to deal with this?

处理这个问题的最佳方法是什么?

采纳答案by aqwert

You can't pass a static type to a method as a parameter because then it would have to be instantiated, and you can't create an instance of a staticclass.

您不能将静态类型作为参数传递给方法,因为这样就必须对其进行实例化,并且您无法创建static类的实例。

回答by Primary Key

The best deal is definitely to remove the last parameter. Since type is static you don't need a reference to an instance and you can refer to its members from your function body.

最好的交易肯定是删除最后一个参数。由于类型是静态的,您不需要对实例的引用,您可以从函数体中引用其成员。

回答by aqwert

You can wrap static types around and interface or another non-static class and add that as the parameter. Not ideal but a way around it. Or simply just reference the static type in the method body itself

您可以将静态类型包装在接口或其他非静态类周围,并将其添加为参数。不理想,而是一种解决方法。或者只是简单地引用方法体本身中的静态类型

回答by Andrew Cooper

Use a different type for the argument.

为参数使用不同的类型。

A method argument need to be of a type that can accept a reference to an instance, so it can't be a static class.

方法参数必须是可以接受对实例的引用的类型,因此它不能是静态类。

回答by Andrew Cooper

It doesn't look like you even use that parameter in your method. You should just remove it because MediaTypeNamescannot be instantiated anyway.

看起来您甚至没有在方法中使用该参数。您应该删除它,因为MediaTypeNames无论如何都无法实例化。

回答by csblo

It's not recommended but you can simulate use of Static classes as parameters. Create an Instance class like this :

不建议这样做,但您可以模拟使用静态类作为参数。创建一个这样的实例类:

public class Instance
{

    public Type StaticObject { get; private set; }

    public Instance(Type staticType)
    {
        StaticObject = staticType;
    }

    public object Call(string name, params object[] parameters)
    {
        MethodInfo method = StaticObject.GetMethod(name);
        return method.Invoke(StaticObject, parameters);
    }

    public object Call(string name)
    {
        return Call(name, null);
    }

}

Then your function where you would use the static class :

然后是您将使用静态类的函数:

    private static void YourFunction(Instance instance)
    {
        instance.Call("TheNameOfMethodToCall", null);
    }

For instance.Call :

例如.Call:

  • The first parameter is the name of the method of your static class to call
  • The second parameter is the list of arguments to pass to the method.
  • 第一个参数是要调用的静态类的方法的名称
  • 第二个参数是传递给方法的参数列表。

And use like this :

并像这样使用:

    static void Main(string[] args)
    {

        YourFunction(new Instance(typeof(YourStaticClass)));

        Console.ReadKey();

    }

回答by Nikolai

Send a static class as the type of the parameter and then give it a variable name for use in the function. This works because the new variable is a reference to the static class. It is necessary to address the global variable problem. If you use a static class as a variable inside a method, you need to pass it in as a parameter, to avoid the global variable issue. This is basic structured programming 101 from the 80's.

发送一个静态类作为参数的类型,然后给它一个变量名以供在函数中使用。这是有效的,因为新变量是对静态类的引用。有必要解决全局变量问题。如果在方法中使用静态类作为变量,则需要将其作为参数传入,以避免全局变量问题。这是 80 年代的基本结构化编程 101。