字符串变量自动变为"null"

Jef*_*fnl 0 .net c# reflection

我遇到了一些奇怪的东西,我创建了一个带有反射的类的实例,它传递了几个参数,一个是变量:ipAddress,当在构造函数中创建实例时,变量存储在一个字段中,但是一旦构造函数完成后,我(在调试器中)返回到我创建实例的行,我在类中检查并且字段'ipAddress'已更改为null.这怎么可能?

这是课程的一部分:

public class moxa_nport_5110
{
    string instanceName;
    Delegate triggerCallBackMethod;

    private BPUConsole bpuConsole { get; set; }

    TcpIpServer server;
    string ipAddress;

    public moxa_nport_5110(Delegate TriggerCallBackMethod, Delegate Callback, params object[] CtorParam)
    {
        #region Initialize
        triggerCallBackMethod = TriggerCallBackMethod;
        instanceName = (string)CtorParam[0];
        string ipAddress = (string)CtorParam[1];
        int Port = (int)CtorParam[2];
        bpuConsole = new BPUConsole(Callback, instanceName);
        #endregion

        server = new TcpIpServer("10.100.184.140", 8888, false);
        server.OnDataReceived += new TcpIpServer.ReceiveEventHandler(server_OnDataReceived);
        server.OnClientConnected += new TcpIpServer.InfoEventHandler(server_OnClientConnected);
        server.OnClientDisconnected += new TcpIpServer.InfoEventHandler(server_OnClientDisconnected);
        server.OnAbnormalConnectionDisconnect += new TcpIpServer.InfoEventHandler(server_OnAbnormalConnectionDisconnect);
        server.AddClient(ipAddress, 1);
    }

    public void SendData(byte[] Data)
    {
        server.SendData(ipAddress, Data);
    }
Run Code Online (Sandbox Code Playgroud)

这是我创建实例的行:

driverInterface = Activator.CreateInstance(driverType, tempParam);  //create an instance of the driver
Run Code Online (Sandbox Code Playgroud)

所以当我返回这里时,字段ipAddress的值为null.

编辑:

Field是同样的价值,但没有加入:

在此输入图像描述

dog*_*ose 6

string ipAddress;

public moxa_nport_5110(Delegate TriggerCallBackMethod, Delegate Callback, params object[] CtorParam)
{
    #region Initialize
    triggerCallBackMethod = TriggerCallBackMethod;
    instanceName = (string)CtorParam[0];
    string ipAddress = (string)CtorParam[1];
}
Run Code Online (Sandbox Code Playgroud)

String是类的一个字段,因此您需要使用设置该值this..您只是创建一个名为ipAdress的"新"字符串,它会遮挡该字段.使用this.ipAdress = ...,而不是在构造函数中.