C# 在C中从另一个类调用私有函数#
我有一个名为C# 在C中从另一个类调用私有函数#,c#,C#,我有一个名为Scenario2\u Client.xaml.cs的程序,它具有以下功能: namespace SDKTemplate { public sealed partial class Scenario2_Client : Page { private MainPage rootPage = MainPage.Current; // code // this is the function I would like to
Scenario2\u Client.xaml.cs
的程序,它具有以下功能:
namespace SDKTemplate
{
public sealed partial class Scenario2_Client : Page
{
private MainPage rootPage = MainPage.Current;
// code
// this is the function I would like to call
private void RemoveValueChangedHandler()
{
ValueChangedSubscribeToggle.Content = "Subscribe to value changes";
if (subscribedForNotifications)
{
registeredCharacteristic.ValueChanged -= Characteristic_ValueChanged;
registeredCharacteristic = null;
subscribedForNotifications = false;
}
}
// ...
}
}
EchoClient client = new EchoClient()
client.somefunction();
client.somefunction();
然后我在另一个文件(但在同一个项目中)中添加了一个名为EchoClient.cs
的类,该类包含以下代码:
namespace SDKTemplate
{
class EchoClient
{
public void TcpClient()
{
try
{
TcpClient client = new TcpClient("139.169.63.130", 9999);
StreamReader reader = new StreamReader(client.GetStream());
StreamWriter writer = new StreamWriter(client.GetStream());
string s = string.Empty;
while (!s.Equals("Exit"))
{
Console.WriteLine("TCP Client connected....");
Console.WriteLine("Enter a string or number to send to the server: ");
s = Console.ReadLine();
writer.WriteLine(s);
writer.Flush();
string server_string = reader.ReadLine();
Console.WriteLine(server_string);
}
reader.Dispose();
writer.Dispose();
client.Dispose();
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
internal class Console
{
internal static string ReadLine()
{
throw new NotImplementedException();
}
internal static void WriteLine(string v)
{
throw new NotImplementedException();
}
internal static void WriteLine(Exception e)
{
throw new NotImplementedException();
}
internal class TcpClient
{
private string v1;
private int v2;
public TcpClient(string v1, int v2)
{
this.v1 = v1;
this.v2 = v2;
}
internal void Dispose()
{
throw new NotImplementedException();
}
internal Stream GetStream()
{
throw new NotImplementedException();
}
}
}
EchoClient client = new EchoClient()
client.somefunction();
client.somefunction();
有没有办法从这个类调用这个函数
EchoClient client = new EchoClient()
client.somefunction();
client.somefunction();
如果是公开的,我会这样做:
EchoClient client = new EchoClient()
client.somefunction();
client.somefunction();
…但是由于此方法是私有的,我应该如何访问它 可以使用反射调用私有方法,如下所示
EchoClient client = new EchoClient()
client.somefunction();
client.somefunction();
var iMethod
= client.GetType().GetMethod("somefunction",
BindingFlags.NonPublic | BindingFlags.Instance);
iMethod.Invoke(client, new object[]{});
可以使用反射调用私有方法,如下所示
EchoClient client = new EchoClient()
client.somefunction();
client.somefunction();
var iMethod
= client.GetType().GetMethod("somefunction",
BindingFlags.NonPublic | BindingFlags.Instance);
iMethod.Invoke(client, new object[]{});
我不知道为什么@Codor被否决了,但这里有一个更具体的答案。首先,我使用私有方法创建一个类:
EchoClient client = new EchoClient()
client.somefunction();
client.somefunction();
public class PrivateFunction
{
private int _age;
public PrivateFunction(int age)
{
_age = age;
}
private int DoSomethingPrivate(string parameter)
{
Debug.WriteLine($"Parameter: {parameter}, Age: {_age}");
return _age;
}
}
我创建了一个方法,该方法接受参数并返回一个整数来显示所有可能性
EchoClient client = new EchoClient()
client.somefunction();
client.somefunction();
那么我叫它:
EchoClient client = new EchoClient()
client.somefunction();
client.somefunction();
var type = typeof(PrivateFunction);
var func = type.GetMethod("DoSomethingPrivate", BindingFlags.Instance | BindingFlags.NonPublic);
var obj = new PrivateFunction(12);
var ret = func.Invoke(obj, new[] {"some parameter"});
Debug.WriteLine($"Function returned {ret}");
我在输出中得到了这一点(证明发生了一些事情):
EchoClient client = new EchoClient()
client.somefunction();
client.somefunction();
如果要重复调用同一函数(可能使用不同的对象),请将
MethodInfo
对象保存在func
中。它是不可变的,可以重复使用。我不确定@Codor为什么被否决,但这里同样的答案更加充实了。首先,我使用私有方法创建一个类:
EchoClient client = new EchoClient()
client.somefunction();
client.somefunction();
public class PrivateFunction
{
private int _age;
public PrivateFunction(int age)
{
_age = age;
}
private int DoSomethingPrivate(string parameter)
{
Debug.WriteLine($"Parameter: {parameter}, Age: {_age}");
return _age;
}
}
我创建了一个方法,该方法接受参数并返回一个整数来显示所有可能性
EchoClient client = new EchoClient()
client.somefunction();
client.somefunction();
那么我叫它:
EchoClient client = new EchoClient()
client.somefunction();
client.somefunction();
var type = typeof(PrivateFunction);
var func = type.GetMethod("DoSomethingPrivate", BindingFlags.Instance | BindingFlags.NonPublic);
var obj = new PrivateFunction(12);
var ret = func.Invoke(obj, new[] {"some parameter"});
Debug.WriteLine($"Function returned {ret}");
我在输出中得到了这一点(证明发生了一些事情):
EchoClient client = new EchoClient()
client.somefunction();
client.somefunction();
如果要重复调用同一函数(可能使用不同的对象),请将
MethodInfo
对象保存在func
中。它是不可变的,并且可以重复使用。如果你需要在外部访问它,为什么它首先是私有的?反射或更改访问修饰符。@Broots Waymb哦,我没有将其设置为私有的。Scenario2是微软在git上直接提供的一个例子。微软将该方法私有化可能有一个很好的原因。要问一些老套的留言板问题:为什么要这样做?你想要实现什么?也就是说,可以使用反射调用私有方法。如果您需要在外部访问它,为什么它首先是私有的?反射或更改访问修饰符。@Broots Waymb好吧,我没有将其设置为私有的。Scenario2是微软在git上直接提供的一个例子。微软将该方法私有化可能有一个很好的原因。要问一些老套的留言板问题:为什么要这样做?你想要实现什么?也就是说,可以使用反射调用私有方法。那么这段代码将放在scenario2或EchoClient中?使用System.Reflection?那么这段代码会出现在scenario2或EchoClient中?使用System.Reflection?六羟甲基三聚氰胺六甲醚。。。有些人不喜欢使用反射来调用私有方法。为什么对这个答案和@Codor’s投反对票。这直接回答了“在C#中从另一个类调用私有函数”的问题。据我所知,这是唯一的答案。也许有人对这样做有哲学上的反对意见?在这段视频中:[]他在课堂外使用反射访问私有方法,结果被否决了。这样做通常是不可取的。您正在打破原始开发人员的假设,因此您正在进入未经测试的领域。然而,有时你需要这样做。使用反射(类似于@Codor和我发布的内容)是实现这一点的方法。谢谢你的回答。但是,我如何遵循这一点,在我的问题中使用它呢?例如,假设场景2是我的私有函数,然后我转到EchoClient类并调用您提到的方法?我的私有函数
类对应于您的场景2_客户端
类。我的DoSomethingPrivate
方法与您的RemoveValueChangedHandler
方法(即您要调用的私有方法)相对应。无论您想在哪里调用它,都要获取Scenario2\u客户端的类型,然后获取与RemoveValueChangedHandler
(使用GetMethod
)和Invoke
对应的MethodInfo
。Hmmm。。。有些人不喜欢使用反射来调用私有方法。为什么对这个答案和@Codor’s投反对票。这直接回答了“在C#中从另一个类调用私有函数”的问题。据我所知,这是唯一的答案。也许有人对这样做有哲学上的反对意见?在这段视频中:[]他在课堂外使用反射访问私有方法,结果被否决了。这样做通常是不可取的。您正在打破原始开发人员的假设,因此您正在进入未经测试的领域。然而,有时你需要这样做。使用反射(类似于@Codor和我发布的内容)是实现这一点的方法。谢谢你的回答。但是,我如何遵循这一点,在我的问题中使用它呢?例如,假设场景2是我的私有函数,然后我转到EchoClient类并调用您提到的方法?我的私有函数
类对应于您的场景2_客户端
类。我的DoSomethingPrivate
方法与您的RemoveValueChangedHandler
方法(即您要调用的私有方法)相对应。无论您想在哪里调用它,都要获取Scenario2\u客户端的类型,然后获取与RemoveValueChangedHandler
(使用GetMethod
)和调用它对应的MethodInfo
。
EchoClient client = new EchoClient()
client.somefunction();
client.somefunction();