How to get Type name of a CallerMember
c#
Solution
It isn't foolproof, but the convention with .NET is to have one type per file and to name the file the same as the type. Our tooling also tends to enforces this convention i.e. Resharper & Visual Studio.
Therefore it should be reasonable to infer the type name from the file path.
public class MyClass
{
public void MyMethod([CallerFilePath]string callerFilePath = null, [CallerMemberName]string callerMemberName = null)
{
var callerTypeName = Path.GetFileNameWithoutExtension(callerFilePath);
Console.WriteLine(callerTypeName);
Console.WriteLine(callerMemberName);
}
}
Problem
I got this class ``` public class fooBase { public List<MethodsWithCustAttribute> MethodsList; public bool fooMethod([CallerMemberName]string membername ="")) { //This returns a value depending of type and method } public void GetMethods() { // Here populate MethodsList using reflection } } ``` And This Attribue Class ``` // This attribute get from a database some things, then fooMethod check this attribute members public class CustomAttribute { public string fullMethodPath; public bool someThing ; public bool CustomAttribute([CallerMemberName]string membername ="") { fullMethodPath = **DerivedType** + membername // I need here to get the type of membername parent. // Here I want to get CustClass, not fooBase } } ``` Then I have this ``` public class CustClass : fooBase { [CustomAttribute()] public string method1() { if (fooMethod()) { .... } } } ``` I need the Type name of the CallerMember, there is something like [CallerMemberName] to get the Type of class owner of the Caller ?