Is it possible to cast an enum to another object?
I have enumarated a list of blob containers. I’d like to implicitly cast the enum type into the named CloudBlobContainer
Here is an example of what i’ve tried
public static class BlobContainerCasts
{
public static implicit operator CloudBlobContainer(EnumTypes.BlobContainerNames BlobContainerName)
{
return CloudStorageAccount.DevelopmentStorageAccount.CreateCloudBlobClient().GetContainerReference(Enum.GetName(typeof(EnumTypes.BlobContainerNames), BlobContainerName));
}
}
Unfortunately, it won’t compile because it doesn’t respect the rule about User-defined conversion (it must convert to or from the enclosing type)
So, is there a way to do it without creating a method like this one?
public CloudBlobContainer GetBlobContainer(EnumTypes.BlobContainerNames BlobContainerName)
{...}
Thank you for your help.
Based on breischl answere, here is the solution: