github / github/codeql

Getting derived class type in C#

Đang mở
#10,342 1 bình luận 0 reaction 0 người được giao Xem trên GitHub
C# question
Ngôn ngữ chính
CodeQL
Star
10.1k
Fork
2.1k
Merge trung bình
2 ngày 15 giờ
Pull request đã merge (30 ngày)
141

Mô tả

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!

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.