Get Caller Information for property in c#





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















Wanted to know how to get caller information for a property



for a method it is easy



public void TraceMessage([CallerMemberName] string memberName = "") {
Console.Println(memberName);
}


and you will get who called the method.



I want the same thing for a property



public MySqlConnection Connection { get; set; }


I tried getting the caller name by calling a function from the getter like this



public Connection connection { get { TraceMessage()
return _someVariable;}
set; }


But by doing this the TraceMessage prints




Connection




as the method name



Is there any way to either pass a parameter to the getter or something else to achieve this?










share|improve this question


















  • 1





    You always can access the Callstack. but be sure that you really need it, because it may cause performance issue. Maybe you could redesign your software.

    – gofal3
    Jan 3 at 7:11






  • 1





    like that: var t = new System.Diagnostics.StackTrace(); var callingMethodName = t.GetFrame(1).GetMethod().Name;

    – gofal3
    Jan 3 at 7:15











  • Ya the thing is I really need it without causing any performance degradation. Hence would avoid using stackTrace. any other idea?

    – pratikvasa
    Jan 3 at 7:30











  • Redesign it so that you expose the connection through a GetConnection() -method and then use CallerMemberName. If you insist on using properties you need to use stacktrace. Otherwise I would suggest adding a good logging framework into your design and forget these types of design choices.

    – aQsu
    Jan 3 at 7:42






  • 1





    sorry, no. the [callerMemberName] is so fast because it is a compiler feature. that menas in IL it is a normal parameter call with a constant string, because the c#-compiler inserts the name of the function if there is no explicit value. In properties the compiler has no way to do similar. So If you really need it, you have to do runtime analysis via stacktrace :-(

    – gofal3
    Jan 3 at 7:42


















0















Wanted to know how to get caller information for a property



for a method it is easy



public void TraceMessage([CallerMemberName] string memberName = "") {
Console.Println(memberName);
}


and you will get who called the method.



I want the same thing for a property



public MySqlConnection Connection { get; set; }


I tried getting the caller name by calling a function from the getter like this



public Connection connection { get { TraceMessage()
return _someVariable;}
set; }


But by doing this the TraceMessage prints




Connection




as the method name



Is there any way to either pass a parameter to the getter or something else to achieve this?










share|improve this question


















  • 1





    You always can access the Callstack. but be sure that you really need it, because it may cause performance issue. Maybe you could redesign your software.

    – gofal3
    Jan 3 at 7:11






  • 1





    like that: var t = new System.Diagnostics.StackTrace(); var callingMethodName = t.GetFrame(1).GetMethod().Name;

    – gofal3
    Jan 3 at 7:15











  • Ya the thing is I really need it without causing any performance degradation. Hence would avoid using stackTrace. any other idea?

    – pratikvasa
    Jan 3 at 7:30











  • Redesign it so that you expose the connection through a GetConnection() -method and then use CallerMemberName. If you insist on using properties you need to use stacktrace. Otherwise I would suggest adding a good logging framework into your design and forget these types of design choices.

    – aQsu
    Jan 3 at 7:42






  • 1





    sorry, no. the [callerMemberName] is so fast because it is a compiler feature. that menas in IL it is a normal parameter call with a constant string, because the c#-compiler inserts the name of the function if there is no explicit value. In properties the compiler has no way to do similar. So If you really need it, you have to do runtime analysis via stacktrace :-(

    – gofal3
    Jan 3 at 7:42














0












0








0








Wanted to know how to get caller information for a property



for a method it is easy



public void TraceMessage([CallerMemberName] string memberName = "") {
Console.Println(memberName);
}


and you will get who called the method.



I want the same thing for a property



public MySqlConnection Connection { get; set; }


I tried getting the caller name by calling a function from the getter like this



public Connection connection { get { TraceMessage()
return _someVariable;}
set; }


But by doing this the TraceMessage prints




Connection




as the method name



Is there any way to either pass a parameter to the getter or something else to achieve this?










share|improve this question














Wanted to know how to get caller information for a property



for a method it is easy



public void TraceMessage([CallerMemberName] string memberName = "") {
Console.Println(memberName);
}


and you will get who called the method.



I want the same thing for a property



public MySqlConnection Connection { get; set; }


I tried getting the caller name by calling a function from the getter like this



public Connection connection { get { TraceMessage()
return _someVariable;}
set; }


But by doing this the TraceMessage prints




Connection




as the method name



Is there any way to either pass a parameter to the getter or something else to achieve this?







c#






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 3 at 7:08









pratikvasapratikvasa

1,0421217




1,0421217








  • 1





    You always can access the Callstack. but be sure that you really need it, because it may cause performance issue. Maybe you could redesign your software.

    – gofal3
    Jan 3 at 7:11






  • 1





    like that: var t = new System.Diagnostics.StackTrace(); var callingMethodName = t.GetFrame(1).GetMethod().Name;

    – gofal3
    Jan 3 at 7:15











  • Ya the thing is I really need it without causing any performance degradation. Hence would avoid using stackTrace. any other idea?

    – pratikvasa
    Jan 3 at 7:30











  • Redesign it so that you expose the connection through a GetConnection() -method and then use CallerMemberName. If you insist on using properties you need to use stacktrace. Otherwise I would suggest adding a good logging framework into your design and forget these types of design choices.

    – aQsu
    Jan 3 at 7:42






  • 1





    sorry, no. the [callerMemberName] is so fast because it is a compiler feature. that menas in IL it is a normal parameter call with a constant string, because the c#-compiler inserts the name of the function if there is no explicit value. In properties the compiler has no way to do similar. So If you really need it, you have to do runtime analysis via stacktrace :-(

    – gofal3
    Jan 3 at 7:42














  • 1





    You always can access the Callstack. but be sure that you really need it, because it may cause performance issue. Maybe you could redesign your software.

    – gofal3
    Jan 3 at 7:11






  • 1





    like that: var t = new System.Diagnostics.StackTrace(); var callingMethodName = t.GetFrame(1).GetMethod().Name;

    – gofal3
    Jan 3 at 7:15











  • Ya the thing is I really need it without causing any performance degradation. Hence would avoid using stackTrace. any other idea?

    – pratikvasa
    Jan 3 at 7:30











  • Redesign it so that you expose the connection through a GetConnection() -method and then use CallerMemberName. If you insist on using properties you need to use stacktrace. Otherwise I would suggest adding a good logging framework into your design and forget these types of design choices.

    – aQsu
    Jan 3 at 7:42






  • 1





    sorry, no. the [callerMemberName] is so fast because it is a compiler feature. that menas in IL it is a normal parameter call with a constant string, because the c#-compiler inserts the name of the function if there is no explicit value. In properties the compiler has no way to do similar. So If you really need it, you have to do runtime analysis via stacktrace :-(

    – gofal3
    Jan 3 at 7:42








1




1





You always can access the Callstack. but be sure that you really need it, because it may cause performance issue. Maybe you could redesign your software.

– gofal3
Jan 3 at 7:11





You always can access the Callstack. but be sure that you really need it, because it may cause performance issue. Maybe you could redesign your software.

– gofal3
Jan 3 at 7:11




1




1





like that: var t = new System.Diagnostics.StackTrace(); var callingMethodName = t.GetFrame(1).GetMethod().Name;

– gofal3
Jan 3 at 7:15





like that: var t = new System.Diagnostics.StackTrace(); var callingMethodName = t.GetFrame(1).GetMethod().Name;

– gofal3
Jan 3 at 7:15













Ya the thing is I really need it without causing any performance degradation. Hence would avoid using stackTrace. any other idea?

– pratikvasa
Jan 3 at 7:30





Ya the thing is I really need it without causing any performance degradation. Hence would avoid using stackTrace. any other idea?

– pratikvasa
Jan 3 at 7:30













Redesign it so that you expose the connection through a GetConnection() -method and then use CallerMemberName. If you insist on using properties you need to use stacktrace. Otherwise I would suggest adding a good logging framework into your design and forget these types of design choices.

– aQsu
Jan 3 at 7:42





Redesign it so that you expose the connection through a GetConnection() -method and then use CallerMemberName. If you insist on using properties you need to use stacktrace. Otherwise I would suggest adding a good logging framework into your design and forget these types of design choices.

– aQsu
Jan 3 at 7:42




1




1





sorry, no. the [callerMemberName] is so fast because it is a compiler feature. that menas in IL it is a normal parameter call with a constant string, because the c#-compiler inserts the name of the function if there is no explicit value. In properties the compiler has no way to do similar. So If you really need it, you have to do runtime analysis via stacktrace :-(

– gofal3
Jan 3 at 7:42





sorry, no. the [callerMemberName] is so fast because it is a compiler feature. that menas in IL it is a normal parameter call with a constant string, because the c#-compiler inserts the name of the function if there is no explicit value. In properties the compiler has no way to do similar. So If you really need it, you have to do runtime analysis via stacktrace :-(

– gofal3
Jan 3 at 7:42












1 Answer
1






active

oldest

votes


















1














You get the name of the property because it is the previous method in the stack trace. To trace it in the property you can use System.Diagnostics.StackTrace:



using System.Diagnostics;
.
.
public Connection connection
{
get
{
Console.WriteLine(new StackTrace().GetFrame(1).GetMethod().Name);
return _connection;
}
}


Example can be found in this link



Furthermore if you want to create a separate method for that you can create a method that gets the frame before the frame. The frames are indexed so that current method/property frame is 0, the caller is 1, caller of the caller is 2 and so forth.



public void LogCaller()
{
Console.WriteLine(new StackTrace().GetFrame(2).GetMethod().Name);
}


And then call that method from the property.






share|improve this answer





















  • 1





    I would only use this to trace a problem if all else fails, not as a good design example though.

    – aQsu
    Jan 3 at 7:27












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54017776%2fget-caller-information-for-property-in-c-sharp%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You get the name of the property because it is the previous method in the stack trace. To trace it in the property you can use System.Diagnostics.StackTrace:



using System.Diagnostics;
.
.
public Connection connection
{
get
{
Console.WriteLine(new StackTrace().GetFrame(1).GetMethod().Name);
return _connection;
}
}


Example can be found in this link



Furthermore if you want to create a separate method for that you can create a method that gets the frame before the frame. The frames are indexed so that current method/property frame is 0, the caller is 1, caller of the caller is 2 and so forth.



public void LogCaller()
{
Console.WriteLine(new StackTrace().GetFrame(2).GetMethod().Name);
}


And then call that method from the property.






share|improve this answer





















  • 1





    I would only use this to trace a problem if all else fails, not as a good design example though.

    – aQsu
    Jan 3 at 7:27
















1














You get the name of the property because it is the previous method in the stack trace. To trace it in the property you can use System.Diagnostics.StackTrace:



using System.Diagnostics;
.
.
public Connection connection
{
get
{
Console.WriteLine(new StackTrace().GetFrame(1).GetMethod().Name);
return _connection;
}
}


Example can be found in this link



Furthermore if you want to create a separate method for that you can create a method that gets the frame before the frame. The frames are indexed so that current method/property frame is 0, the caller is 1, caller of the caller is 2 and so forth.



public void LogCaller()
{
Console.WriteLine(new StackTrace().GetFrame(2).GetMethod().Name);
}


And then call that method from the property.






share|improve this answer





















  • 1





    I would only use this to trace a problem if all else fails, not as a good design example though.

    – aQsu
    Jan 3 at 7:27














1












1








1







You get the name of the property because it is the previous method in the stack trace. To trace it in the property you can use System.Diagnostics.StackTrace:



using System.Diagnostics;
.
.
public Connection connection
{
get
{
Console.WriteLine(new StackTrace().GetFrame(1).GetMethod().Name);
return _connection;
}
}


Example can be found in this link



Furthermore if you want to create a separate method for that you can create a method that gets the frame before the frame. The frames are indexed so that current method/property frame is 0, the caller is 1, caller of the caller is 2 and so forth.



public void LogCaller()
{
Console.WriteLine(new StackTrace().GetFrame(2).GetMethod().Name);
}


And then call that method from the property.






share|improve this answer















You get the name of the property because it is the previous method in the stack trace. To trace it in the property you can use System.Diagnostics.StackTrace:



using System.Diagnostics;
.
.
public Connection connection
{
get
{
Console.WriteLine(new StackTrace().GetFrame(1).GetMethod().Name);
return _connection;
}
}


Example can be found in this link



Furthermore if you want to create a separate method for that you can create a method that gets the frame before the frame. The frames are indexed so that current method/property frame is 0, the caller is 1, caller of the caller is 2 and so forth.



public void LogCaller()
{
Console.WriteLine(new StackTrace().GetFrame(2).GetMethod().Name);
}


And then call that method from the property.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 3 at 7:31

























answered Jan 3 at 7:25









aQsuaQsu

617613




617613








  • 1





    I would only use this to trace a problem if all else fails, not as a good design example though.

    – aQsu
    Jan 3 at 7:27














  • 1





    I would only use this to trace a problem if all else fails, not as a good design example though.

    – aQsu
    Jan 3 at 7:27








1




1





I would only use this to trace a problem if all else fails, not as a good design example though.

– aQsu
Jan 3 at 7:27





I would only use this to trace a problem if all else fails, not as a good design example though.

– aQsu
Jan 3 at 7:27




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54017776%2fget-caller-information-for-property-in-c-sharp%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

Npm cannot find a required file even through it is in the searched directory