Thanks for your reply Amdrit,
Based on the complete code sample you provided, I've also tested it on my
side. I think the output is as expected.
In the static constructor of "Logger" class, the "GetCallingAssembly" will
return "MyLogger" assembly because the static constructor is called by .NET
runtime's internal code which is marked as the assembly itself(Rather than
the "ReflectionTest" app assembly).
And in the "LogInfo" method, the "GetCallingAssembly" will return
"ReflectionTest" because it is the immediate caller of the LogInfo method.
Also, if you call "GetEntryAssembly" it will return the assembly that
contains the Main entry point, that's the "ReflectionTest" app assembly
here. You can also see this chain correctly via the CallStack in
debugger's callstack window.
However, for unmanaged VB6 client, it is not a managed application, the
.NET runtime won't mark it as part of the calling assembly chain or entry
point, that's why you will not get it as Entry point assembly.
Why do you need to cache the calling AssemblyName in static constructor? I
think at runtime, it is possible that many different classes in different
assemblies may call "LogInfo" method and the "CallingAssmebly" may also
vary from time to time. It is reasonable to call "GetCallingAssembly" in
"LogInfo" method if you do need to get the assembly who invoke that method.
Microsoft MSDN Online Support Lead
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
Get notification to my posts through email? Please refer to http://msdn.microsoft.com/subscripti...ult.aspx#notif
This posting is provided "AS IS" with no warranties, and confers no rights.
>Thread-Topic: Retrieve name of VB6 calling app via reflection from C# COM
>Subject: Re: Retrieve name of VB6 calling app via reflection from C# COM in
>Date: Tue, 15 Apr 2008 04:55:01 -0700
>In the real app I don't know hat the calling assembly is nor do I know
>any types it contains.
>Here is a simple example of the issue.
>// Calling assembly
> class Program
> static void Main(string args)
> public class Logger
> static string callingAssembly = string.Empty;
> static Logger()
> // This is where I need to know who called. The first call is
>to LogInfo Below
> // that causes this constructor to be called.
> // What I need to know is the assembly that called LogInfo
> string caller = Assembly.GetCallingAssembly().FullName; //
> string s;
> Assembly a = Assembly.GetEntryAssembly();
> if (null != a)
> s = a.FullName; // this gives me 'ReflectionTester', but
>when called from VB6. a is null in that case
> public static void LogInfo(string info)
> if (callingAssembly.Equals(string.Empty))
> callingAssembly = Assembly.GetCallingAssembly().FullName;
>//This gives 'Logger' but it is too late by the time this is run
>"Steven Cheng [MSFT]" wrote:
>> Hi Amdrit,
>> As for the "GetCallingAssembly" method, it should return the immediate
>> caller of the current method. I'm not sure whether there is anything
>> in your applicaiton/assmeblies that may cause the problem, but if what
>> want to do is get reference to a certain assembly, you can consider
>> the a type in the assembly to reference it. e.g.
>> "MyAssembly.MyType" is a known type defined in the target assemly I
>> to get reference
>> Type tp =typeof(MyAssembly.MyType);
>> Assembly asm = tp.Assembly;
>> Steven Cheng
>> Microsoft MSDN Online Support Lead
>> Delighting our customers is our #1 priority. We welcome your comments
>> suggestions about how we can improve the support we provide to you.
>> feel free to let my manager know what you think of the level of service
>> provided. You can send feedback directly to my manager at:
>> Get notification to my posts through email? Please refer to
>> This posting is provided "AS IS" with no warranties, and confers no
>> >From: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= <QSIDeveloper@xxxxxx>
>> >References: <8A940661-D84F-4602-B1E5-5477864E6962@xxxxxx>
>> >Subject: Re: Retrieve name of VB6 calling app via reflection from C#
>> >Date: Mon, 14 Apr 2008 10:43:14 -0700
>> >When I call GetCallingAssembly It does not fail (my bad) but I get the
>> >Current assembly, in the prev description that would be the C# logging
>> >assembly rather than the C# COM interface assembly as I had expected.
>> >I also tried applying MethodImplAttribute attribute with
>> >MethodImplOptions.NoInlining to the C# logging assembly constructor but
>> >had no affect.
>> >I also tried GetEntryAssembly but that return null when run with a VB6
>> >executable calling the COM interface.
>> >I would like not to need an additional parameter for the name because
>> >will impact existing code. I would prefer a silent/automatic name
>> >"amdrit" wrote:
>> >> What do you mean "fails"? Do you get an exception, or do you get and
>> >> string, or do you get a string that makes no sense?
>> >> It would seem to me that you are on the right track, and since the VB
>> >> is the ultimately the caller, is should come back with it's name if
>> >> However, since you have the middle interface, why not require the
>> >> name itself to the interface and just pass in App.Name there?
>> >> "QSIDeveloper" <QSIDeveloper@xxxxxx> wrote in message
>> >> news:8A940661-D84F-4602-B1E5-5477864E6962@xxxxxx
>> >> >I have a C# logging assembly with a static constructor and methods
>> >> > called from another C# Assembly that is used as a COM interface for
>> >> > Application. Ideally I need to build a file name based on the name
>> >> > VB6 application. A second choice would be a file name based on the
>> >> > interface assembly. I have tried calling
>> >> > but
>> >> > this fails when I use the VB6 client. Is there a way to get this
>> >> > information
>> >> > at runtime?