确定在C#中调用对象类型 [英] Determine Calling Object Type in C#
问题描述
不管这是否是一个好主意,是有可能实现一个接口其中执行功能是知道调用对象的类型?
Regardless of whether or not this is a good idea, is it possible to implement an interface where the executing function is aware of the calling object's type?
class A
{
private C;
public int doC(int input)
{
return C.DoSomething(input);
}
}
class B
{
private C;
public int doC(int input)
{
return C.DoSomething(input);
}
}
class C
{
public int DoSomething(int input)
{
if(GetType(CallingObject) == A)
{
return input + 1;
}
else if(GetType(CallingObject) == B)
{
return input + 2;
}
else
{
return input + 3;
}
}
}
在我看来,这样的是一个不好的编程习惯(因为参数不改变,但输出会),但除了这可能吗?
It seems to me like this is a bad coding practice (because the parameters don't change, but the output would) but aside from that is it possible?
我在一种情况是我要一些特定类型的能够调用一个一定的功能,但不能排除访问的功能。
我想到有一个类型参数
I'm in a situation were I want a few specific types to be able to call a certain function, but I can't exclude access to the function. I thought about having a "type" parameter
DoSomething(int input, Type callingobject)
但是,没有任何保证调用对象将用gettype(这一点),而不是的GetType(b),以欺骗A b不顾自己的类型。
But there's no guarantee that the calling object would use GetType(this), as opposed to GetType(B) to spoof a B regardless of their own type.
这会是简单(相对简单)为检查调用栈?
Would this be as simple (relatively simple) as examining the callstack?
修改
请投了JaredPar的答复(如果你想),以及约翰Feminella的。我不能标志着两个答案为接受,我接受了约翰Feminella的答案,因为它是具体是什么我问,而不是贾里德的回答这带来了我以前没有考虑的解决方案。
Please vote up JaredPar's answer (if you want) as well as John Feminella's. I could not mark both answers as accepted, and I accepted John Feminella's answer because it is specifically what I asked for, as opposed to Jared's answer which brought up a solution which I had not previously considered.
推荐答案
首先,是的,这是一个可怕的想法做这,打破各种的设计原则。你一定要考虑一个替代方案,如果这就是开放的,像简单地使用多态 - 这似乎是它可以重构单派遣一个相当明确的情况下
First, yes, it's a terrible idea to do this and breaks all kinds of solid design principles. You should definitely consider an alternative approach if that's open, like simply using polymorphism -- this seems like it can be refactored to a pretty clear case of single dispatch.
其次,是的,这是可能的。使用 System.Diagnostics.StackTrace
走栈;然后得到相应的的StackFrame
一个级别。然后确定哪些的方法是使用 GetMethod调用者()
上的StackFrame
。需要注意的是建立一个堆栈跟踪是一种潜在的昂贵的操作,并且有可能为你的方法的调用者掩盖这里的东西是真正来源。
Secondly, yes, it's possible. Use System.Diagnostics.StackTrace
to walk the stack; then get the appropriate StackFrame
one level up. Then determine which method was the caller by using GetMethod()
on that StackFrame
. Note that building a stack trace is a potentially expensive operation, and it's possible for callers of your method to obscure where things are really coming from.
编辑:从OP这一评论使得它非常清楚,这很可能是一个通用或多态性的方法。 @devinb ,你可能要考虑一个新的问题,提供有关你想要做什么更多的细节,我们可以看它是否很适合一个很好的解决方案。
This comment from the OP makes it pretty clear that this could probably be a generic or polymorphic method. @devinb, you might want to consider making a new question that provides more detail about what you're trying to do, and we can see if it lends itself well to a good solution.
短的版本是,我最终会
具有30或40的分别为
简单地关闭一个或两行相同的功能。 - devinb(12秒前)
The short version is that I would end up have 30 or 40 identical functions that were simply off by one or two lines. – devinb (12 secs ago)
这篇关于确定在C#中调用对象类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!