11

I notice that .NET core doesn't allow myObj.GetType().GetProperties() as no GetProperties method exists. Is there another way to obtain the properties of a class through reflection?

7
  • 6
    myObj.GetType().GetTypeInfo().GetProperties() Commented Nov 13, 2016 at 22:02
  • 1
    Have you tried with this pkg nuget.org/packages/System.Reflection.TypeExtensions ? Commented Nov 13, 2016 at 22:02
  • See also: stackoverflow.com/questions/36118978/… Commented Nov 13, 2016 at 22:03
  • Nkosi No GetTypeInfo method appears to exist in .NET core Commented Nov 13, 2016 at 22:04
  • 1
    It does, it's just an extra package. That's the philosophy in .NET Core - that's why the project is named "Core". Reflection isn't part of the core, since it means horrible dependencies, which make .NET Native a lot less useful. Commented Nov 13, 2016 at 22:05

2 Answers 2

20

It seems that myObj.GetType().GetProperties() IS valid. I just had to bring in System.Reflection by using System.Reflection.

Sign up to request clarification or add additional context in comments.

Comments

11

Just to sum up to anyone else, just adding using System.Reflection to the top of the page is not enough. You will have to add the System.Reflection.TypeExtensions NuGet package as described in the question comments.

PM> Install-Package System.Reflection.TypeExtensions -Version 4.3.0

4 Comments

1/ This is a comment to a comment written as an answer, 2/ No you don't. 3./ This garnered an upvote surprisingly quickly.
Yes you do. I had the same issue in a .NET Standard 1.6 project and only the combination above solved it for me. In .NET Core unfortunately namespaces are now spread over multiple assemblies.
1/ Yeah you do 2/Seems like if you're answering your own question (and not a complete answer) you're on shaky ground about getting picky with upvotes... I upvoted both of you... Together it's a complete answer
@MoslemBenDhaou the question refers to ".Net Core" not .Net Standard

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.