Search Unity

error CS1061: 'Type' does not contain a definition for 'IsEnum'

Discussion in 'Scripting' started by archor, Jan 17, 2018.

  1. archor

    archor

    Joined:
    Sep 18, 2015
    Posts:
    9
    error CS1061: 'Type' does not contain a definition for 'IsEnum' and no extension method 'IsEnum' accepting a first argument of type 'Type' could be found

    Here's the Line of Code that kicks the error ...

    if (prop.FieldType.IsEnum) prop.SetValue(aClass, System.Enum.Parse(prop.FieldType, aDictionary[prop.Name].s));

    - Any Hints ?

    ????
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    6,654
    So I'm not certain what exactly is causing your problem.

    But 'IsEnum' is defined as a member:
    https://msdn.microsoft.com/en-us/library/system.type.isenum(v=vs.110).aspx

    And you seem to be using it correctly. I have to assume 'FieldType' is a Type, even the error suggests that... unless you defined some class somewhere called 'Type' as well. Check to make sure you don't have a class/struct somewhere named Type.

    I will say, I have seen the compiler sometimes (not often, but sometimes) throw an error mistakenly for a line of code because a line preceding it somewhere had an error. And the compiler just identified the wrong line.
     
  3. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,491
    By any chance, are you targeting UWP or something? Some things have been moved, removed or renamed for apps that use .Net Core. This also includes some types and members that belong to the System.Reflection namespace, but also IO, Web, Pipes...
     
    BlackPete and lordofduct like this.
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    6,654
    Yep... Suddoha is correct, IsEnum was moved to the TypeInfo for .net core.

    You must first call 'GetTypeInfo()' on the type and than check IsEnum:
    Code (csharp):
    1. prop.FieldType.GetTypeInfo().IsEnum
     
  5. archor

    archor

    Joined:
    Sep 18, 2015
    Posts:
    9
    Yes Target = UWP / XBOX ...

    Tried -

    if (prop.FieldType.GetTypeInfo().IsEnum) prop.SetValue(aClass, System.Enum.Parse(prop.FieldType, aDictionary[prop.Name].s));

    but getting Error -

    CS1061: Type `System.Type' does not contain a definition for `GetTypeInfo' and no extension method `GetTypeInfo' of type `System.Type' could be found. Are you missing `NUnit.Compatibility' using directive?

    ?????
     
  6. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    969
    I haven't built for UWP in a while, but you might be missing the "using System.Reflection" directive. GetTypeInfo() is an extension method, so it's not built into the Type class itself.

    Disclaimer: I haven't tested it myself, so I don't actually know if System.Reflection is available for UWP builds...
     
  7. archor

    archor

    Joined:
    Sep 18, 2015
    Posts:
    9
    Thanks for the Hints, I added using System.Reflection, but, still getting same error.

    ???
     
  8. HECer

    HECer

    Joined:
    Mar 17, 2013
    Posts:
    20
    how did you solve it?
     
  9. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,491
    Never noticed this wasn't resolved. :eek:

    Which scripting runtime version are you using?

    The type TypeInfo was added with the .Net Framework version 4.5, many of the additional extension methods as well.
    Unity's .Net 3.5 equivalent simply does not support this type, neither the extension methods (which makes sense - if the type doesn't exist, anything dependent on it cannot exist either).

    There are two options now:

    1) Switch to .Net 4+ equivalent. This should support the TypeInfo type and should also have the additional extension methods for System.Type, in particular the one you need GetTypeInfo().

    2) You cannot or do not want to switch: Add platform-dependent compilation.
    The editor can use the old IsEnum property on System.Type when wrappen in appropriate compilation symbols.
    The platforms that require UWP or any other compatibility would use the alternative call to GetTypeInfo().IsEnum or whatever is required.

    I strongly recommend to write your own extension method for that, which hides the ugly symbols in places where you need to check that. You'll then only need to do the above within that extension method and you'll be fine no matter how often you need it in your code.
     
  10. HECer

    HECer

    Joined:
    Mar 17, 2013
    Posts:
    20
    Thank you for your response.

    I tried to switch UWP to .Net 4.6, but this didn't resolve the issue.
    Tried to install the extension nuget-package System.Reflection.TypeExtension in Visual Studio without success,
    it said i try to use a .net 4 package in .net 3.5,
    sooooo

    I changed this:
    Code (CSharp):
    1. typeof(T).IsEnum
    to
    Code (CSharp):
    1. typeof(T).GetType() is IEnumerable
    i am not sure if it's correct/possible, but the compiler doesn't cry anymore.
     
  11. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,491
    A type check for IEnumerable is not what you're looking for. Any collection, array and many more types are also implementors of the IEnumerable interface (it's the interface that enables things to be iterated, using an IEnumerator - it's an iterator functionality).

    Where did you change a setting to .Net 4.6?
     
  12. davidlannan

    davidlannan

    Joined:
    Jul 11, 2013
    Posts:
    45
    The answer that worked fine for me:
    Code (CSharp):
    1. using System.Reflection;
    You don't need any nuget packages etc. That sounds like you are building a separate library/dll to Unity.