C# 扩展方法可以访问私有变量吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1548320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can C# extension methods access private variables?
提问by Geo
Is it possible to access an object's private variables using an extension method?
是否可以使用扩展方法访问对象的私有变量?
采纳答案by M4N
No. You can do the same in an extension method as in a "normal" static method in some utility class.
不。您可以在扩展方法中执行与某些实用程序类中的“普通”静态方法相同的操作。
So this extension method
所以这个扩展方法
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.)
(当然,您可以在任一方法中使用一些反射来访问私有成员。但我想这不是这个问题的重点。)
回答by Yuriy Faktorovich
No, unless you give some kind of access to them through public properties or a proxy pattern.
不,除非您通过公共属性或代理模式授予对它们的某种访问权限。
回答by Darin Dimitrov
No:
不:
public class Foo
{
private string bar;
}
public static class FooExtensions
{
public static void Test(this Foo foo)
{
// Compile error here: Foo.bar is inaccessible due to its protection level
var bar = foo.bar;
}
}
回答by Abhijeet Patel
An extension method is essentially a static method so all you have access to are the public members of the instance on which the extension method is invoked on
扩展方法本质上是一个静态方法,因此您可以访问的是在其上调用扩展方法的实例的公共成员
回答by Zaid Masud
No it cannot.
不,它不能。
However, you will be interested to know that the other answers are incorrect in saying that normal static methods cannot access private fields. A static method can access private non-static member fields in its own class.The following code is perfectly valid and shows a static method accessing a private field:
但是,您将有兴趣知道其他答案在说正常静态方法无法访问私有字段方面是不正确的。静态方法可以访问自己类中的私有非静态成员字段。以下代码完全有效,并显示了访问私有字段的静态方法:
public class Foo
{
private bool _field;
public static bool GetField(Foo foo)
{
return foo._field;
}
}
Now... back to your question. You might think that an extension method should be able to do the same thing, given the (non-existent) "equivalence" to static methods that other answers claim exists. However, you cannot declare extension methods inside a nested class. So if you try to do the following:
现在……回到你的问题。您可能认为扩展方法应该能够做同样的事情,因为其他答案声称存在的静态方法(不存在)“等效”。但是,您不能在嵌套类中声明扩展方法。因此,如果您尝试执行以下操作:
public class Foo
{
private bool _field;
public static class Extensions
{
public static bool GetField(this Foo foo)
{
return foo._field;
}
}
}
You will get a compile error saying
你会得到一个编译错误说
Extension method must be defined in a top level static class; Extensions is a nested class
扩展方法必须在顶级静态类中定义;扩展是一个嵌套类
Note that, interestingly enough, removing the this
keyword causes the code to compile fine. The reasons for this are discussed here:
请注意,有趣的是,删除this
关键字会导致代码编译正常。原因如下:
回答by Rimbimbambo
If you own the class that you are extending, you an always declare the class partial, then extend the class & have access to all the private members in a different file... But you wouldn't really be using extension methods.
如果你拥有你正在扩展的类,你总是声明类部分,然后扩展类并可以访问不同文件中的所有私有成员......但你不会真正使用扩展方法。
回答by Bruno Zell
Use Reflection
使用反射
Not recommended, but you could possibly access any private variable of any type using another extension method like so:
不推荐,但您可以使用另一种扩展方法访问任何类型的任何私有变量,如下所示:
public static T GetFieldValue<T>(this object obj, string name) {
var field = obj.GetType().GetField(name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
return (T)field?.GetValue(obj);
}
And then access a private field of an arbitrary type:
然后访问任意类型的私有字段:
Foo foo = new Foo();
string privateBar = foo.GetFieldValue<string>("_bar");