开发者

Restricting the call on a method

I need to restrict the calling of a method from a particular meth开发者_开发问答od.

For example:

I've a function like GetData() and I need its calling method's signature to be void SomeFUnction(string a, String b) otherwise the calling of GetData should not be allowed at compile time itself.


No, you cannot do it at compile time.


If you just want to restrict that "GetData" could be called from "SomeFuncion(...)", then you can easily do that by encapsulating your method in a class (instance/static) and making the "GetData" method private, and only call it from your "SomeFunction(...)". How else could you restrict this at compile time!!.

Though, I believe you are not sharing the exact problem statement here. Instead, you are sharing a thought that you percieve as a possible solution to your problem!!.

Sharing the exact problem statement will fetch you better answers.


This does not create a compile time break. However, if test coverage is hitting all calls of GetData(), this'll do what you are looking for by breaking tests.

public void GetData()
{
   // last method up the stack trace
   var callingMethod = new StackTrace().GetFrame(1).GetMethod();

   var returnType = callingMethod.ReturnType;
   // throw exception if not okay

   var callingMethodParameters = callingMethod.GetParameters();
   // throw exception if not okay

   // GetData implementation
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜