25
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?

2
  • 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. Commented Mar 7, 2012 at 2:53
  • stackoverflow.com/questions/5858591/… Commented Oct 1, 2013 at 2:53

8 Answers 8

34

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.

Sign up to request clarification or add additional context in comments.

3 Comments

This is what I figured, what would be a good way to solve this problem in my case?
@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.
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.
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();

    }

Comments

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.

Comments

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.

Comments

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.

Comments

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.

1 Comment

You can't pass a reference to a static class. You access the static members via the type name.
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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.