17

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?

2 Answers 2

13

As of writing this, GetProperties() is now:

typeof(Object).GetTypeInfo().DeclaredProperties;

1
  • 4
    DeclaredProperties only returns properties declared on the type itself, excluding those inherited from parents
    – AunAun
    Jun 14, 2018 at 13:26
13

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;

1
  • That's right. But I think you have a typo in your answer. It is typeof(Object).GetTypeInfo().GetProperties(); Feb 3, 2017 at 20:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.