C# - Static Types Cannot Be Used as Type Arguments

C# - static types cannot be used as type arguments

This is deliberate.

Static classes try to prevent inappropriate use, so in almost all situations, you can't use them in situations where you'd normally want an instance of the type... and that includes type arguments.

See section "Static classes" of the C# 6 spec for the very limited set of situations in which you can refer to static class types.

C# Static types cannot be used as parameters

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.

Static classes can be used as type arguments via reflection

As stated in the comment from BJ Myers, the CLR has no knowledge of "static" classes. The compiler errors are there to prevent you from doing things that can cause serious issues. There are almost always ways around most errors like this (in this case using reflection), but the error when trying to pass the static class as a parameter is a good indication that you should not be doing what you are doing unless you know very well what the repercussions are.

In this case, you should be asking yourself, why are you wanting to pass a static class? Since a static class can have no references to data or other objects, there is no point to pass this. If you need to call functions on a static class without having an explicit reference to it, you can use reflection to invoke its methods. Here is an answer explaining that:

Invoking static methods with reflection

MudBlazor: 'CategoryTypes.Element': static types cannot be used as type arguments

As commented by Jesse Good, I got caught out by the example having a class of Element which also existed in the MudBlazor Library as a static class! I replaced that class with my own, i.e.:

        private TableGroupDefinition<Subcategory> _groupDefinition = new()
{
GroupName = "Category",
Indentation = false,
Expandable = true,
IsInitiallyExpanded = false,
Selector = (c) => c.CategoryId
};

Static types cannot be used as parameters

Just rename your static HtmlHelper class to HtmlHelperExtensions.

Using static type member to ensure type-safety in a non-static class

You could wrap JobStatus to make it "type safe" but it looks like a bit of an overkill:

public sealed class JobStatusWrapper
{
public static readonly JobStatusWrapper Completed
= new JobStatusWrapper(JobStatus.Completed);

public static readonly JobStatusWrapper Failed
= new JobStatusWrapper(JobStatus.Failed);

public static readonly JobStatusWrapper Stopped
= new JobStatusWrapper(JobStatus.Stopped);

private readonly string description;
private JobStatusWrapper(string description) {
Debug.Assert(!string.IsNullOrEmpty(description));
this.description = description; }

public static implicit operator string(JobStatusWrapper status)
=> status.description;
}

And now you'd use it:

public class JobOutput
{
//...
public JobStatusWrapper JobStatus { get; set; }
}

And there is no way to pass in or get a JobStatusWrapper that doesn't have one of the underlying values defined in JobStatus (except null). Also, the implicit operator makes the wrapper usable anywhere the JobStatus options are.

Show Files trough viewmodel isn't possible because of static types cannot be used as type arguments

Your problem is exactly what error say. You can't use static class as property type.
If you really want to set file to ViewModel you should use FileResult or FileContentResult or just byte[].



Related Topics



Leave a reply



Submit