Skip to content

Simple source only reflection library with default interface property support

License

Notifications You must be signed in to change notification settings

dennisdoomen/reflectify

Repository files navigation

Reflectify - Reflection extensions without causing dependency pains

Coveralls branch GitHub Repo stars GitHub contributors GitHub last commit GitHub commit activity open issues

What's this about?

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.

What's so special about that?

Nothing really, but it offers that functionality through a content-only NuGet package. 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.

How do I use it?

Simple, to get the properties of a type, add the Reflectify NuGet package 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 and HasAttributeInHierarchy
  • IsPublic, IsInternal or IsAbstract 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 and GetClosedGenericInterfaces
  • Various HasAttribute, HasAttributeInHierarchy and GetMatchingAttributes overloads, with and without additional filtering.
  • OverridesEquals to determine if a type implements value semantics
  • IsSameOrInherits to see if a type is the same as or derives from another (open-generic) type
  • IsCompilerGenerated and HasFriendlyName 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