![]() |
1
5
Ah ok, I see what you mean now...
But it has nothing to do with boxing... |
![]() |
2
5
Thomas 为这个特定的实例提供正确的答案。 That being said, there is no "general purpose" way to prevent users from attempting to cast types to their "actual" type instead of an interface you provide them. The closest thing you can do is use types that are internal within your assembly, and not visible to the caller. This will prevent the user from being able to use your type directly (at least without resorting to reflection in elevated trust situations). 不过,总的来说,我建议不要担心这一点。任何将您的类型强制转换为未记录的内部实现细节类型的用户都只是在自找麻烦,我不会尝试用代码来防止这种情况发生。这样做只会对您自己造成损害,因为您正在降低代码的可维护性。 |
![]() |
3
1
In this specific case where you return an IEnumerable Thomas's solution using yield is probably the best. In the general case where the returned interface might be anything (like IDisposable as you mentioned in your edit) the only way to achieve what you want would be to create a wrapper class that implements the given interface using delegation (e.g. a class MyDisposable that holds an
|
|
Robert King · Unity C#语法问题-转换位置 1 年前 |
![]() |
JBryanB · 如何从基本抽象类访问类属性 1 年前 |
|
law · 检查答案按钮的输入字符串格式不正确 2 年前 |
![]() |
i_sniff_ket · 在unity之外使用unity类 2 年前 |