Eltons.ReflectionKit 1.2.0

dotnet add package Eltons.ReflectionKit --version 1.2.0
NuGet\Install-Package Eltons.ReflectionKit -Version 1.2.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Eltons.ReflectionKit" Version="1.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Eltons.ReflectionKit --version 1.2.0
#r "nuget: Eltons.ReflectionKit, 1.2.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Eltons.ReflectionKit as a Cake Addin
#addin nuget:?package=Eltons.ReflectionKit&version=1.2.0

// Install Eltons.ReflectionKit as a Cake Tool
#tool nuget:?package=Eltons.ReflectionKit&version=1.2.0

Eltons.ReflectionKit

Rewrite of code I posted here https://stackoverflow.com/a/13318056/222054

This is an extension method on MethodInfo called GetSignature(bool invokable)

Install via nuget

  • Eltons.ReflectionKit NuGet

Features

  • Can generate signatures that are valid in class definitions
    • public static void GetResult(string a, string b)
  • Can generate signatures that can be invoked
    • GetResult(a, b)
  • Fully qualifies type names
    • Exception becomes System.Exception
  • Simplifies types with keywords
    • String becomes string, Nullable<T> becomes T?
  • Handles nested generics
    • The following works public List<Dictionary<string, string>> TransformInputs<T>(List<string> inputs)
  • Works with extension methods
    • public static string GetName(this User user) or if invokable GetName()
  • Works with protection modifiers
    • internal public private protected internal protected

How To

using System;
using Eltons.ReflectionKit;
using System.Collections.Generic;

namespace MyProject
{
    public class App
    {
        public static void Main(string[] args) {
            var type = typeof(App);
            var method = type.GetMethod(nameof(TestMethod));

            // Outputs `public string TestMethod(string firstParam)`
            Console.WriteLine(method.GetSignature(false));
        }
        
        public string TestMethod(string firstParam) {
            throw new NotImplementedException();
        }
    }
}

Examples

For ideas on how to use this, please view the Examples

I've added the Examples.cs class here for a quick reference

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

using System;
using Eltons.ReflectionKit;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Eltons.ReflectionKit.Tests
{
    [TestClass]
    public class Examples
    {
        [TestMethod]
        public void Example_Signature() {
            var type = typeof(Examples);
            var method = type.GetMethod(nameof(TestMethod));

            var signature = method.GetSignature(false);

            Assert.AreEqual("public string TestMethod(string firstParam)", signature);
        }

        [TestMethod]
        public void Example_Signature_Invokable() {
            var type = typeof(Examples);
            var method = type.GetMethod(nameof(TestMethod));

            var signature = method.GetSignature(true);

            Assert.AreEqual("TestMethod(firstParam)", signature);
        }

        [TestMethod]
        public void Example_Signature_Generics() {
            var type  = typeof(Examples);
            var method = type.GetMethod(nameof(TestMethod2));

            var signature = method.GetSignature(false);

            Assert.AreEqual("public System.Collections.Generic.List<string> TestMethod2<T>(string firstParam, T secondParam)", signature);
        }

        [TestMethod]
        public void Example_Signature_Generics_Invokable() {
            var type  = typeof(Examples);
            var method = type.GetMethod(nameof(TestMethod2));

            var signature = method.GetSignature(true);

            Assert.AreEqual("TestMethod2<T>(firstParam, secondParam)", signature);
        }

        [TestMethod]
        public void Example_Signature_Generics_Nested() {
            var type = typeof(Examples);
            var method = type.GetMethod(nameof(TestMethod3));

            var signature = method.GetSignature(false);

            Assert.AreEqual("public void TestMethod3(System.Action<System.Action<System.Action<string>>> firstParam)", signature);
        }

        [TestMethod]
        public void Example_Signature_Nullable() {
            var type = typeof(Examples);
            var method = type.GetMethod(nameof(TestMethod4));

            var signature = method.GetSignature(false);

            Assert.AreEqual("public void TestMethod4(int? firstParam)", signature);
        }

        [TestMethod]
        public void Example_Signature_Accessor() {
            var type = typeof(Examples);
            var method = type.GetMethod(nameof(TestMethod5), BindingFlags.Static | BindingFlags.NonPublic);

            var signature = method.GetSignature(false);

            Assert.AreEqual("internal static void TestMethod5()", signature);
        }

        [TestMethod]
        public void Example_Signature_ExtensionMethod() {
            var type = typeof(TestExtensionMethods);
            var method = type.GetMethod(nameof(TestExtensionMethods.ExtensionMethod));

            var signature = method.GetSignature(false);

            Assert.AreEqual("public static string ExtensionMethod(this string firstParam, bool secondParam)", signature);
        }

        [TestMethod]
        public void Example_Signature_ExtensionMethod_Invokable() {
            var type = typeof(TestExtensionMethods);
            var method = type.GetMethod(nameof(TestExtensionMethods.ExtensionMethod));

            var signature = method.GetSignature(true);

            Assert.AreEqual("ExtensionMethod(secondParam)", signature);
        }

        public string TestMethod(string firstParam) {
            throw new NotImplementedException();
        }

        public List<string> TestMethod2<T>(string firstParam, T secondParam) {
            throw new NotImplementedException();
        }

        public void TestMethod3(System.Action<System.Action<System.Action<string>>> firstParam) {
            throw new NotImplementedException();
        }

        public void TestMethod4(Nullable<int> firstParam) {
            throw new NotImplementedException();
        }

        internal static void TestMethod5() {

        }
    }

    public static class TestExtensionMethods
    {
        public static string ExtensionMethod(this string firstParam, bool secondParam) {
            throw new NotImplementedException();
        }
    }
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.0 3,359 1/6/2022
1.1.0 283 11/17/2021

Added support for Constructors (Thanks https://github.com/3DExtended )