Skip to content

Latest commit

 

History

History
143 lines (102 loc) · 8.17 KB

README.md

File metadata and controls

143 lines (102 loc) · 8.17 KB


Reflectify

Reflection extensions without causing dependency pains

About

What's this?

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 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.

Who created this?

My name is Dennis Doomen and I'm a Microsoft MVP and Principal Consultant at Aviva Solutions with 28 years of experience under my belt. As a software architect and/or lead developer, I specialize in designing full-stack enterprise solutions based on .NET as well as providing coaching on all aspects of designing, building, deploying and maintaining software systems. I'm the author of Fluent Assertions, a popular .NET assertion library, Liquid Projections, a set of libraries for building Event Sourcing projections and I've been maintaining coding guidelines for C# since 2001.

Contact me through Email, BlueSky, Twitter/X or Mastadon

How do I use it?

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 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.

Download

This library is available as a NuGet package on https://nuget.org. To install it, use the following command-line:

dotnet add package Reflectify

Contributors

Your contributions are always welcome! Please have a look at the contribution guidelines first.

contrib.rocks image

(Made with contrib.rocks)

Versioning

This library uses Semantic Versioning to give meaning to the version numbers. For the versions available, see the tags on this repository.

Credits

This library wouldn't have been possible without the following tools, packages and companies:

  • Nuke - Smart automation for DevOps teams and CI/CD pipelines
  • Rider - The world's most loved .NET and game dev IDE
  • xUnit - Community-focused unit testing tool for .NET.
  • Coverlet - Cross platform code coverage for .NET
  • Polysharp - Generated, source-only polyfills for C# language features
  • GitVersion - From git log to SemVer in no time
  • ReportGenerator - Powerful code coverage visualization

Support the project

You may also like

License

This project is licensed under the MIT License - see the LICENSE.md file for details.