Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET vNext Core CLR missing type.IsPrimitive

I am trying to migrate a web app to ASP.Net vNext with the eventual aim of getting it running on Linux.

The app has a lot of reflection code and I must be missing some dependencies as I am getting compile errors on code such as this

Type.IsPrimitive, Type.GetConstructor Type.GetMethod Type.GetTypeArray Error CS1061 'Type' does not contain a definition for 'IsPrimitive' and no extension method 'IsPrimitive' accepting a first argument of type 'Type' could be found (are you missing a using directive or an assembly reference?)

Error CS1061 'Type' does not contain a definition for 'GetMethod' and no extension method 'GetMethod' accepting a first argument of type 'Type' could be found (are you missing a using directive or an assembly reference?)

Error CS1061 'Type' does not contain a definition for 'GetProperties' and no extension method 'GetProperties' accepting a first argument of type 'Type' could be found (are you missing a using directive or an assembly reference?)

Error CS1061 'Type' does not contain a definition for 'GetInterface' and no extension method 'GetInterface' accepting a first argument of type 'Type' could be found (are you missing a using directive or an assembly reference?)

I have the following dependencies in my project.json files

"frameworks" : {
    "aspnetcore50" : { 
        "dependencies": {
            "System.Runtime": "4.0.20-beta-22416",
            "System.Linq": "4.0.0.0-beta-22605",
            "System.Reflection": "4.0.10.0-beta-22605",
            "System.Reflection.Primitives": "4.0.0.0-beta-22605",
            "System.Runtime.Extensions": "4.0.10.0-beta-22605",
            "System.Reflection.Extensions": "4.0.0.0-beta-22605"
        }

The following compiles fine under VS 2013 and .Net 4.5 but wont compile in VS 2015 using the dependencies above

using System;
using System.Reflection;


namespace Project1
{
    public class Class1
    {
        public Class1()
        {
            Type lBaseArrayType = typeof(Array);
            Type lStringType = typeof(string);
            string[] lStringArray = new string[1];
            if (lStringType.IsPrimitive)
            {
            }
            ConstructorInfo lConstructor = lStringType.GetConstructor(new Type[0]);
            MethodInfo lMethod = lStringType.GetMethod("Equals");
            Type[] lTArray = Type.GetTypeArray(lStringArray);
            PropertyInfo[] lProps = lStringType.GetProperties();
        }
    }
}
like image 581
Mark Munro Avatar asked Mar 12 '15 17:03

Mark Munro


2 Answers

You can use the static method System.Reflection.IntrospectionExtensions.GetTypeInfo() to get the same information.

    var arrayType = typeof(Array);
    var stringType = typeof(string);
    var stringArray = new string[1];
    var stringTypeInfo = stringType.GetTypeInfo();
    if (stringTypeInfo.IsPrimitive)
    {
    }

    ConstructorInfo lConstructor = stringTypeInfo.DeclaredConstructors.Where(x => x.GetParameters().Any() == false).FirstOrDefault();
    MethodInfo lMethod = stringTypeInfo.DeclaredMethods.Where(x => x.Name == "Equals").FirstOrDefault();
    Type[] lTArray = stringArray.Where(x => x != null).Select(x => x.GetType()).Distinct().ToArray();
    PropertyInfo[] lProps = stringTypeInfo.DeclaredProperties.ToArray();
like image 68
Mario Arturo Avatar answered Oct 20 '22 17:10

Mario Arturo


If you're using aspnetcore .IsPrimitive is available, but not as a member of Type. You'll find it under TypeInfo, which can be accessed by calling the GetTypeInfo() method of Type. In your example, it would be:

lStringType.GetTypeInfo().IsPrimitive

Type.GetMethod() is also available, but you'll need to reference the System.Reflection.TypeExtensions package in your project.json file.

Type.GetTypeArray() is missing, but you can easily write a simple linq query to retrieve an array of member types in the array.

Type.GetInterface() is not included, but again using System.Reflection.TypeExtensions will expose another method that generates a Type[] of all implemented interfaces for the type specified.

Type[] types = Type.GetInterfaces()

Type.GetProperties() is available again through the System.Reflection.TypeExtensions library.

like image 31
cygnim Avatar answered Oct 20 '22 17:10

cygnim