2

I'm making a class library project in C#. How to get the name of the project which will call the method from my class library?

I tried with Reflection:

System.Reflection.Assembly.GetExecutingAssembly()

and with

GetCallingAssembly

but it didn't work.

Maheep
  • 5,539
  • 3
  • 28
  • 47
petko_stankoski
  • 10,459
  • 41
  • 127
  • 231

3 Answers3

1

I am looking for this also. File name I can get from StackFrame just like the answer by @Maheep but to get project name is not really straight forward.

A. I just got a simple solution:

  1. Based on the FileName (path) found from StackFrame, iterate for each parent folder.
  2. Find any file with *.csproj (C#)
  3. Optionally open csproj file to ensure the current file <Compile Include="Folder\File.cs" /> is included
  4. The file name of csproj is the project name

B. I found alternative by using pre-build macro $(ProjectDir) and access the result in code here: What's an easy way to access prebuild macros such as $(SolutionDir) and $(DevEnvDir) from code in C#?

Community
  • 1
  • 1
CallMeLaNN
  • 8,328
  • 7
  • 59
  • 74
0

If I understand your question directly, this is not possible to do in code. The only way to do this is with static analysis of the code.

Resharper has this ability - To find out where a particular class/method/property is used, you can right click on the declaration and select "Find Usages". This is a very handy feature :)

But that only works of the code calling your method is known (in the same solution). It wont work when third parties consume your library.

What exactly are you trying to achieve here? If your method needs to identify callers, you should add this as a requirement (ie add a parameter containing the caller identity).

public void MyMethod()
{
    // I need name of caller project, but how?
}

public void MyMethod(String callerProject)
{
    // People who call this method know the name of their own project :)
}
MattDavey
  • 8,897
  • 3
  • 31
  • 54
  • @Meheep yes you could inspect the stack trace to find the name of the method which called 'this' method. Without knowing what the OP is trying to achieve it's difficult to know if that would solve his problem. I still think if his method requires a caller identity it should put that requirement up front in the method signature as a parameter :) – MattDavey Feb 09 '12 at 09:42
  • 1
    Name is what OP is asking for. – Maheep Feb 09 '12 at 09:45
  • @Maheep he said name of the *project* - this would not be in the stack trace. We need to know what the OP is trying to do... – MattDavey Feb 09 '12 at 09:46
0

You will have to use StackTrace Class for this. StackTrace class has GetFrame method which will give you calling method name. This will return MethodBase class object which has property DeclaringType. Using this type information you can get assembly details as well.

private void YouCalledMethod()
{
    StackTrace stackTrace = new StackTrace();
    StackFrame stackFrame = stackTrace.GetFrame(1);
    Assembly assembly = stackFrame.GetMethod().DeclaringType.Assembly;
    //use this assembly object for your requirement.
}

Look at this How to print the current Stack Trace in .NET without any exception? question also.

Community
  • 1
  • 1
Maheep
  • 5,539
  • 3
  • 28
  • 47
  • yes this will get assembly name - this is not necessarily the same as *project* name. OP needs to provide more information about what he's trying to do. – MattDavey Feb 09 '12 at 09:55