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?

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Kai
  • 732
  • 1
  • 7
  • 23
  • 1
    @Jon I doubt you need to cast here at all and thus you don't need reflection to call a generic method... – Patrick Hofman Apr 18 '17 at 07: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... – Jon Skeet Apr 18 '17 at 08:16

1 Answers1

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.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • 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(), another generic method – Kai Apr 18 '17 at 07:33
  • But you don't ever use the generic type parameter `T`, so why make it generic at all? – Patrick Hofman Apr 18 '17 at 07:34
  • 1
    @Kai If the whole point of the cast is, that you need the IEnumerable interface for some reason, just use `Cast()`... you don't use `T` anyway. – grek40 Apr 18 '17 at 07:36
  • @Patrick: The generic parameter was needed for the cast (or otherwise for OfType) This worked well for 1 item, but now that I have a list I cannot find the correct data to pass as T anymore – Kai Apr 18 '17 at 07:37
  • 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. – Patrick Hofman Apr 18 '17 at 07:38
  • @both: I see the problem now. The problem was indeed that I was using T in the wrong way, while I did not even need it here. Thanks both for your reactions – Kai Apr 18 '17 at 07:41