1

I'm trying to use reflection to manually build a ts file, but can't seem to figure out how to do it the correct way. I have a property which will be a list of enum class names (types). For each of these I will call a method with creates a ts file. Method creating files (start):

public static bool CreateJsonFiles<T>(object item, string filePath = null)
{
    try
    {
        var type = item.GetType();
        var values = Enum.GetValues(type).Cast<T>();
        if (filePath == null)
        {
            return false;
        }

Beforehand I used the T generic parameter, by calling the function like

EnumsBuilder.CreateJsonFiles<LocatieType>($"{Server.MapPath("/")}");

The problem is that I wish to call the method for multiple value now (in an iteration). I've tried two things that both do not work:

  1. Change generic method to remove parameter, and use typeof(item):

    var type = item.GetType();
    var values = Enum.GetValues(type).Cast<type >();
    

    This gives an error on the Cast function.

  2. Try to obtain the type of the items in the list in the iteration and try to pass this along:

    public static List<Type> EnumList { get; set; }
    
     public static void BuildEnums(string serverPath)
     {
        foreach (var item in EnumList)
        {
            var type = item.GetType();         
            CreateJsonFiles<type>(type, serverPath);
        } 
     }
    

    This also gives an error, because this is not allowed either.

Could anyone explain me the correct way to do this?

2
  • 1
    @Jon I doubt you need to cast here at all and thus you don't need reflection to call a generic method... Commented Apr 18, 2017 at 7:30
  • @PatrickHofman: It's a bit of an XY question, yes. I was closing as a duplicate in terms of the question that was actually asked, rather than working out whether the OP really should be taking that route at all... Commented Apr 18, 2017 at 8:16

1 Answer 1

2

I doubt why you would need the Cast at all. The cast will not change the actual type of the data (unless you use conversion operators, which I doubt here). It will only change the type of the variable.

I guess you can use just this:

var values = Enum.GetValues(type);

And for your CreateJsonFiles method, since you don't actually need the generic type parameter, you can make it non-generic instead.

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

6 Comments

This seems to shove the problem to later in the code. At some point I want to enumerate through the collection and the .Cast creates an enumerable allowing this. If I do not use cast I receive an Array, which I seem to only be able to cast back via OfType<T>(), another generic method
But you don't ever use the generic type parameter T, so why make it generic at all?
@Kai If the whole point of the cast is, that you need the IEnumerable<T> interface for some reason, just use Cast<object>()... you don't use T anyway.
@Patrick: The generic parameter was needed for the cast (or otherwise for OfType<T>) This worked well for 1 item, but now that I have a list I cannot find the correct data to pass as T anymore
But you didn't give any reason why you need OfType or Cast and why you can't just replace that with the non-generic type variant.
|

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.