24
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:

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

What is the best way to deal with this?

Tom Gullen
  • 61,249
  • 84
  • 283
  • 456
  • 1
    You are probably wanting to use "string" as the type. MediaTypeNames has a number of classes held within it each with some static properties that return strings. – PlayDeezGames Mar 07 '12 at 02:53
  • http://stackoverflow.com/questions/5858591/c-sharp-static-types-cannot-be-used-as-type-arguments – Mauricio Scheffer Oct 01 '13 at 02:53

8 Answers8

32

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.

  • 1
    This is what I figured, what would be a good way to solve this problem in my case? – Tom Gullen Mar 07 '12 at 02:52
  • 1
    @TomGullen It's a `static` class, so you don't seem to need to instantiate it for your logic. You can just reference the class within your method. You don't need to pass it as a parameter, and you don't even reference the parameter name in your method. Just referencing the class name as `static` will be sufficient. You already do that correctly in your method. Long story short, just take out the parameter and you should be all set. –  Mar 07 '12 at 02:58
  • A class specified as a type parameter cannot be used for instantiatiation unless the type parameter has specified a "new" constraint. So it still doesn't make sense not to be able to pass a static type as parameter without the "new" constraint. – Daniel B Jul 25 '17 at 01:21
7

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 :

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

    }
csblo
  • 1,571
  • 13
  • 20
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.

Primary Key
  • 1,237
  • 9
  • 14
1

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.

Pang
  • 9,564
  • 146
  • 81
  • 122
aqwert
  • 10,559
  • 2
  • 41
  • 61
1

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.

Pang
  • 9,564
  • 146
  • 81
  • 122
Andrew Cooper
  • 32,176
  • 5
  • 81
  • 116
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.

Nikolai
  • 11
0

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);
Semir
  • 7
  • 1
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.