InfoOf

Created: 2012-08-21 10:02
Updated: 2019-03-05 09:30
License: mit
c#

readme.md

Chat on Gitter NuGet Status

This is an add-in for Fody

Icon

Provides methodof, propertyof and fieldof equivalents of typeof.

Usage

See also Fody usage.

NuGet installation

Install the InfoOf.Fody NuGet package and update the Fody NuGet package:

PM> Install-Package Fody
PM> Install-Package InfoOf.Fody

The Install-Package Fody is required since NuGet always defaults to the oldest, and most buggy, version of any dependency.

Add to FodyWeavers.xml

Add <InfoOf/> to FodyWeavers.xml

<?xml version="1.0" encoding="utf-8" ?>
<Weavers>
  <InfoOf/>
</Weavers>

Your Code

var type = Info.OfType("AssemblyName", "MyClass");
var method = Info.OfMethod("AssemblyName", "MyClass", "MyMethod");
var constructor = Info.OfConstructor("AssemblyName", "MyClass");
var getProperty = Info.OfPropertyGet("AssemblyName", "MyClass", "MyProperty");
var setProperty = Info.OfPropertySet("AssemblyName", "MyClass", "MyProperty");
var field = Info.OfField("AssemblyName", "MyClass", "myField");

What gets compiled

var type = typeof(MyClass);
var method = methodof(MyClass.MyMethod);
var constructor = methodof(MyClass..ctor);
var getProperty = methodof(MyClass.get_MyProperty);
var setProperty = methodof(MyClass.set_MyProperty);
var field = fieldof(MyClass.myField);

Specifying Generic Types

The typeName parameter of the Info.Of* methods use the following BNF grammar:

<fullTypeSpec> ::= <typeName> [<genericSpec>]
<genericSpec>  ::= "<" <genericTypes> ">"
<genericTypes> ::= <genericType> ["," <genericTypes>]
<genericType>  ::= <assemblyName> "|" <fullTypeSpec>
<name>         ::= <identifier> [<name>]
<typeName>     ::= <name>
<assemblyName> ::= <name>
<identifier>   ::= <letter> | <digit> | <specialChar>
<letter>       ::= "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | "K" | "L" | "M" |
                   "N" | "O" | "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z" |
                   "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" |
                   "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z"
<digit>        ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
<specialChar>  ::= "." | "`"

To specify a Dictionary<int, string>, the typeName would be System.Collections.Generic.Dictionary``2<mscorlib|System.Int32,mscorlib|System.String>.

Escape Sequences

If the following chars are part of your typeName, they will need to be escaped with a "\": "\", "<", ">", "|", ",".

Also, whitespace is ignored by default, so they also need to be escaped with "\" if they are part of your typeName.

Why not use Expressions

It would also be possible to define members and types using a combination of generics and expressions. This would allow for intellisense at code time. The problem with expressions are as follows

  1. The conversion of expression to "infoof"s is significantly more complex. Making the solution more complex and having a negative impact on compile performance.
  2. Expressions cannot represent non public types or members.
  3. It is difficult to represent all combinations of members using expressions.

But it is not strong typed

Actually it is strong typed, it just does not have intellisense. If any of the strings passed into Info do not map to a type or member it will log a build error and stop the build.

Icon

Information designed by Phil Goodwin from The Noun Project

Cookies help us deliver our services. By using our services, you agree to our use of cookies Learn more