Reflectify offers a bunch of extension methods to provide information such as the properties or fields a type exposes and metadata about those members, and many other details about classes, records and interfaces. It supports all major .NET versions and even understands explicitly implemented properties or properties coming from default interface implementations, a C# 8 feature.
Nothing really, but it offers that functionality through a content-only NuGet package that relies on C# 12. In other words, you can use this package in your own packages, without the need to tie yourself to the Reflectify package. Oh, and it's used by an open-source project with over 400 million downloads.
Simple, to get the properties of a type, add the Reflectify
NuGet package to a project that targets at least C# 12 and use
using Reflectify;
var properties = typeof(SuperClass).GetProperties(
MemberKind.Public | MemberKind.ExplicitlyImplemented | MemberKind.DefaultInterfaceProperties);
You can take any of the options Public
, Internal
, Static
, ExplictlyImplemented
and DefaultInterfaceProperties
.
If you need the fields, use GetFields
(which obviously cannot be explicitly implemented, nor be part of interfaces),
and if you need the members, use GetMembers
. You can also request individual members by name, like
GetProperty("Name", MemberKind.Public)
or GetField("Name", MemberKind.Internal)
.
To get more metadata from a PropertyInfo
, you can use extensions methods like:
IsExplictlyImplemented
IsIndexer
HasAttribute
andHasAttributeInHierarchy
IsPublic
,IsInternal
orIsAbstract
to check either the getter or setters matches the criteria
Similarly, you can find indexers using FindIndexers
, conversion operators through FindImplicitConversionOperators
and FindExplicitConversionOperators
, and methods via FindMethod
, FindParameterlessMethod
and HasMethod
.
Other extension methods act on Type
directly and include:
IsDerivedFromOpenGeneric
andGetClosedGenericInterfaces
- Various
HasAttribute
,HasAttributeInHierarchy
andGetMatchingAttributes
overloads, with and without additional filtering. OverridesEquals
to determine if a type implements value semanticsIsSameOrInherits
to see if a type is the same as or derives from another (open-generic) typeIsCompilerGenerated
andHasFriendlyName
to see if a type is (partially) generated by the compiler, like records, tuples, etc.IsAnonymous
,IsTuple
,IsRecord
,IsRecordClass
,IsRecordStruct
,IsKeyValuePair
to find these types.
Additionally, Reflectify offers some helpers such as
NullableOrActualType
to get the actual type of a nullable type or the type itself if it's not nullable.
Tip
If you like this package, consider sponsoring me through Github Sponsors