2

We were porting a .NET 4.0 class Library to .NET Core 1.1 and struck an issue of limited support for .NET Reflection in the .NET Core CLR.

Can anyone help us with any details related to the .net core equivalent for calling Type.GetProperties and Type.GetCustomAttributes methods on an assembly object?

 Using System.Type;
 Type myType;   
    var prop = myType.GetProperties();
    var attrib = myType.GetCustomAttributes(true);

We can see there is CustomAttributes property, but this doesn't return instances of the custom attributes, but rather metadata about the attributes.

1
  • Call Type.GetTypeInfo first. Commented Jan 11, 2017 at 13:39

1 Answer 1

2

You'll need to use GetTypeInfo like this:

using System;
using System.Reflection;

Type myType = ...;   
TypeInfo myTypeInfo = myType.GetTypeInfo();

var prop = myTypeInfo.GetProperties();
var attrib = myTypeInfo.GetCustomAttributes(true);

When .NET Standard 2.0 and .NET Core 2.0 arrive, my understanding is that you won't need to make this change and can use the original APIs.

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

1 Comment

As an addendum, this document, titled "Porting to .NET Core" explicitly states the changes to System.Type and to do what you're recommending: blogs.msdn.microsoft.com/dotnet/2016/02/10/porting-to-net-core

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.