Skip to content
LosManos edited this page Dec 9, 2022 · 12 revisions

CompulsoryCow.Meta

Meta info helper

Does

Get various meta information.

The problem solved

Getting meta information in C# can be tricky. Some helper method can come in handy.

GetCallingMethod

This method gets information about whatever called your code. It walks the stack which might be expensive.

void MyFirstMethod(){
	MySecondMethod();
}
void MySecondMethod(){
	var callingMethod = CompulsoryCow.ReflectionUtilities.GetCallingMethod();
	//	callingMethod.Name is now "MyFirstMethod".
}

GetClass

static Type GetClass(this object me, string className)

This method returns the System.Type of the class in the parameter. If nothing is found an System.ArgumentException exception is thrown.

static Type GetClassOrNull(Type objectType, string className)

This method retuns the System.Type of the class in the parameter. If nothing is found null is returned.

static Type GetClass(string className)

This method retuns the System.Type of the class in the parameter. If nothing is found an System.ArgumentException exception is thrown.

static Type GetClass(string assemblyName, string @namespace, string className)

This method retrieves a class by its Assembly, Namespace and Class name. If nothing is found an System.ArgumentException exception is thrown. See https://msdn.microsoft.com/en-us/library/w3f99sx1.aspx for how to write more complex class names like nested and generic. Hint: Nested classes are denoted with a + character like "Customer+Address".

GetProperty

This method gets information about the property you are in.

class MyClass{
    public string Title{
	      get{
	          //  Just call with this.GetProperty.
            Log( "The user just called the property" + this.GetProperty().Name );
            return _title;
        }
    }
}
GetPrivate...

GetPrivateField, GetPrivateStaticField, GetPrivateProperty GetPrivateStaticProperty, GetPrivateMethod, GetPrivateStaticMethod

var method = Meta.GetPrivateMethod(anObject, "GetCustomer");
method.Invoke(anObject, new[]{42});

Warning: These methods might be deprecated in the future in favour of ReachIn.

GetPublicProperties

static PropertyInfo[] GetPublicProperties(Type type, bool recurse = false, bool distinct = false)

This method returns PropertyInfo for all public properties for a type. Use recurse for telling it to, recursively, dive into properties. Note: Types can be complex so make sure it can follow yor properties' types, them being classes or enumerations or arrays or whatnot.

Use it like so: var ps = GetPublicProperties(typeof(T), true);

GetCallingMethod

A method retrieving information about the calling method. Warning: This method might be deprecated as it only works properly in debug compile and doesn't behave as expected as it contains the historical where-you've-been but rather where it will go when it returns.

Code

Meta

Clone this wiki locally