github / github/codeql

Getting derived class type in C#

未關閉
#10,342 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
C# question
主要語言
CodeQL
星號
10.1k
分支
2.1k
平均合併
2 天 15 小時
30 天內合併 PR
141

描述

For the following code snippet:

```
class A{
public virtual void someFunc(){}
}
class B : A{
public override void someFunc(){}
}
public A createB(){
return new B();
}
main(){
var b = createB();
b.someFunc();
}
```

Is there a good way to check that b is actually type B and not A? And that b.someFunc() is calling the B implementation?

I've tried getType() on b and it's giving me A, which makes sense since createB() does explicitly return an A type. Current approach is going into the createB() method and having my logic iterate through there but it's becoming a lot more complex than I think it needs to be.

Searched through past issues and seems like my question is similar to #9784 . The suggestion was to use dataflow but wanted to check if there might be an easier way in C#.

Any thoughts/ideas?

Thanks!

貢獻指南

開啟貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。