Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type does not contain a definition for 'GetProperties'

I am migrating a library project to a .net standard and I am getting the following compilation error when I try to use the System.Reflection API to call Type:GetProperties():

Type does not contain a definition for 'GetProperties'

Here it is my project.json:

{
  "version": "1.0.0-*",
  "buildOptions": {
    "debugType": "portable"
  },
  "dependencies": {},
  "frameworks": {
    "netstandard1.6": {
      "dependencies": {
        "NETStandard.Library": "1.6.0"
      }
    }
  }
}

What am I missing?

like image 872
Miguel Gamboa Avatar asked Feb 03 '17 17:02

Miguel Gamboa


People also ask

What is GetProperties C#?

GetProperties() Returns all the public properties of the current Type.

Is Getproperty case sensitive?

Instance | BindingFlags. Static (in Visual Basic, combine the values using Or ) to get it. The search for name is case-sensitive. The search includes public static and public instance properties.


Video Answer


2 Answers

Update: with .NET COre 2.0 release the System.Type come back and so both options are available:

  • typeof(Object).GetType().GetProperties()
  • typeof(Object).GetTypeInfo().GetProperties()

    This one requires adding using System.Reflection;

  • typeof(Object).GetTypeInfo().DeclaredProperties

    Notice that this property returns IEnumerable<PropertyInfo>, not PropertyInfo[] as previous two methods.


Most reflection-related members on System.Type are now on System.Reflection.TypeInfo.

First call GetTypeInfo to get a TypeInfo instance from a Type:

typeof(Object).GetTypeInfo().GetProperties();

Also, don't forget to use using System.Reflection;

like image 65
Set Avatar answered Oct 03 '22 20:10

Set


As of writing this, GetProperties() is now:

typeof(Object).GetTypeInfo().DeclaredProperties;

like image 39
pim Avatar answered Oct 03 '22 19:10

pim