简体   繁体   English

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

[英]C# Static types cannot be used as parameters

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 AttachmentType throws the error:如您所见, MediaTypeNames AttachmentType抛出错误:

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

What is the best way to deal with this?处理这个问题的最佳方法是什么?

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

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();

    }

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.由于类型是静态的,您不需要对实例的引用,您可以从函数体中引用其成员。

You can wrap static types around an 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.或者只是简单地引用方法体本身中的静态类型。

Use a different type for the argument.为参数使用不同的类型。

A method argument needs to be of a type that can accept a reference to an instance, so it can't be a static class.方法参数需要是可以接受对实例的引用的类型,因此它不能是静态类。

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。

A workaround to passing static parameters is to pass it as an object.传递静态参数的一种解决方法是将其作为对象传递。

Function:功能:

public static void HoldKey(object key)
{
   ...
}

Call function:调用函数:

Function(static param);

It doesn't look like you even use that parameter in your method.看起来您甚至没有在方法中使用该参数。 You should just remove it because MediaTypeNames cannot be instantiated anyway.您应该删除它,因为无论如何都无法实例化MediaTypeNames

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM