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

时间:2020-12-19 19:17:21
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?

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

7 个解决方案

#1


16  

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 static class.

您不能将静态类型作为参数传递给方法,因为它必须实例化,并且您不能创建静态类的实例。

#2


5  

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

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

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 :

例如。电话:

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

    }

#3


1  

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

您可以包装静态类型和接口或另一个非静态类,并将其添加为参数。不理想,但绕过它。或者只是简单地引用方法体本身中的静态类型

#4


1  

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.

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

#5


1  

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.

最好的交易肯定是删除最后一个参数。由于type是static,因此您不需要对实例的引用,您可以从函数体中引用它的成员。

#6


0  

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。

#7


0  

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。

#1


16  

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 static class.

您不能将静态类型作为参数传递给方法,因为它必须实例化,并且您不能创建静态类的实例。

#2


5  

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

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

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 :

例如。电话:

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

    }

#3


1  

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

您可以包装静态类型和接口或另一个非静态类,并将其添加为参数。不理想,但绕过它。或者只是简单地引用方法体本身中的静态类型

#4


1  

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.

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

#5


1  

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.

最好的交易肯定是删除最后一个参数。由于type是static,因此您不需要对实例的引用,您可以从函数体中引用它的成员。

#6


0  

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。

#7


0  

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。