C#扩展方法可以访问私有变量? [英] Can C# extension methods access private variables?
本文介绍了C#扩展方法可以访问私有变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有可能使用的扩展方法访问对象的私有变量?
Is it possible to access an object's private variables using an extension method?
推荐答案
没有。你可以做同样的在扩展方法,在一些工具类正常的静态方法。
No. You can do the same in an extension method as in a "normal" static method in some utility class.
所以这个扩展方法
public static void SomeMethod(this string s)
{
// do something with 's'
}
等同于一些像这样的静态辅助方法(至少就可以访问哪些):
is equivalent to some static helper method like this (at least regarding what you can access):
public static void SomeStringMethod(string s)
{
// do something with 's'
}
(当然你可以使用任何一种方法的一些反射来访问私有成员。但我想这不是问题的关键的这个问题。)
(Of course you could use some reflection in either method to access private members. But I guess that's not the point of this question.)
这篇关于C#扩展方法可以访问私有变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文