C# Static types cannot be used as parameters

前端 未结 8 2611
被撕碎了的回忆
被撕碎了的回忆 2021-02-18 13:22
public static void SendEmail(String from, String To, String Subject, String HTML, String AttachmentPath = null, String AttachmentName = null, MediaTypeNames AttachmentTy         


        
相关标签:
8条回答
  • 2021-02-18 13:30

    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);
    
    0 讨论(0)
  • 2021-02-18 13:34

    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.

    0 讨论(0)
  • 2021-02-18 13:38

    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.

    0 讨论(0)
  • 2021-02-18 13:38

    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.

    0 讨论(0)
  • 2021-02-18 13:47

    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.

    0 讨论(0)
  • 2021-02-18 13:53

    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.

    0 讨论(0)
提交回复
热议问题