Micro C# lib for easier reflection.
Install-Package Mirrors
Or visit https://www.nuget.org/packages/Mirrors/ for more details.
Lets start with a basic example:
// just a testing class..
class Annie
{
public string Name;
public string HitBy;
public bool IsOk;
public void TellUs(string what)
{
}
}
static void Main(string[] args)
{
// create annie
Annie annie = new Annie();
// set some fields
Mirrors.Set(annie, "Name", "Annie");
Mirrors.Set(annie, "hitby", "A smooth criminal", ignoreCase: true);
Mirrors.SetFromString(annie, "IsOk", "false");
// get fields
bool isOk = Mirrors.Get<bool>(annie, "IsOk");
// invoke
Mirrors.Invoke(annie, "TellUs", new object[] { "Are you ok?" });
}
Mirrors
is the main API class with all basic functionality:
Set field / property value by name (similar to Python's setattr).
Set field / property value by name, after converting value from string.
Get field / property value by name (similar to Python's getattr).
Return object's classname as string.
Invoke a method by name.
Get all field, property and method names in object.
Parse enum value from string.
MirrorsEx
provide some extra, more specific functionality. Check out class docs (in code) for more details.
Mirrors
may raise the following exceptions:
A field you were trying to access does not exist.
You were trying to write to a read-only property.
You were trying to read from a write-only property.
Wrong type in get value (for example tried to get bool from a string field).
Tried to set value from string but had wrong format(for example tying to set bool from "bla bla" string).
Tried to set value from string to a class that doesn't support that type of converter.
MIT License.