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 未指定任何 CodeQL 文件或测试;请先查看相关 issue #9784 以及此处提到的 C# 数据流指导。完成标准是形成一套有文档记录且达成共识的方法,用于确定运行时派生类型和分派的方法;如果现有支持不足,则完成一项范围明确的 CodeQL 更改。

由索引模型根据 Issue 内容生成。

评估

技术栈
csharp
领域
devtools, security
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
需要澄清
新手友好度
20/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。