Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C#中更改所选类中的变量,而不使用包装器、委托_C#_Pointers_Delegates_Console_Wrapper - Fatal编程技术网

在C#中更改所选类中的变量,而不使用包装器、委托

在C#中更改所选类中的变量,而不使用包装器、委托,c#,pointers,delegates,console,wrapper,C#,Pointers,Delegates,Console,Wrapper,我正在编程我的控制台,我想为变量设置一个控制台操作。用户应该创建新的ConsoleValiableAction和他想要分配给它的过去变量。我需要能够从ConsoleVariableAction类访问此变量 我尝试了很多不同的方法。首先,我尝试创建包装器 public class ConsoleVariableCommand<T> : ConsoleCommand where T : Wrapper { protected Wrapper wrapper; publ

我正在编程我的控制台,我想为变量设置一个控制台操作。用户应该创建新的ConsoleValiableAction和他想要分配给它的过去变量。我需要能够从ConsoleVariableAction类访问此变量

我尝试了很多不同的方法。首先,我尝试创建包装器

public class ConsoleVariableCommand<T> : ConsoleCommand where T : Wrapper 
{
    protected Wrapper wrapper;

    public ConsoleVariableCommand(T wrapper)
    {
        this.wrapper = wrapper;
    }

    public override void Perform (List<string> args)
    {
        if (args.Count > 1)
            wrapper = Utils.FromString<T> (args [1]);
    }

}
公共类ConsoleVariableCommand:ConsoleCommand其中T:Wrapper
{
保护包装;
公共控制台变量命令(T包装器)
{
this.wrapper=包装器;
}
公共覆盖无效执行(列表参数)
{
如果(args.Count>1)
wrapper=Utils.FromString(args[1]);
}
}
这很烦人,因为当我想使用int时,我必须首先创建IntWrapper。第二个选项是通过委托完成函数。然而,即使这样也很烦人。您必须为希望在控制台中包含的每个变量编写方法

public class ConsoleVariableCommand
{
    public delegate void Set(string val);
    protected Set setter;

    public ConsoleVariableCommand(Set setter)
    {
        this.setter = setter;
    }

    public override void Perform (List<string> args)
    {
        if (args.Count > 1)
            setter (args [1]);
    }

}
公共类ConsoleVariableCommand
{
公共委托无效集(字符串val);
保护集设置器;
公共控制台变量命令(设置设置器)
{
this.setter=setter;
}
公共覆盖无效执行(列表参数)
{
如果(args.Count>1)
setter(args[1]);
}
}
然后我也尝试了指针,但它不起作用

public class ConsoleVariableCommand<T> : ConsoleCommand
{
    protected unsafe T variable;

    public unsafe ConsoleVariableCommand (T variable)
    {
        this.variable = &variable;
    }

    public override void Perform (List<string> args)
    {
        if (variable != null && args.Count > 1) 
        {
            variable = Utils.FromString<T>(args[1]);
        }
    }

}
公共类ConsoleVariableCommand:ConsoleCommand
{
受保护的不安全T变量;
公共不安全控制台变量命令(T变量)
{
this.variable=&variable;
}
公共覆盖无效执行(列表参数)
{
if(变量!=null&&args.Count>1)
{
变量=Utils.FromString(args[1]);
}
}
}

那么,有没有一种方法可以在没有包装器、委托或指针的情况下做到这一点呢?

在您的场景中,将变量保留在单个字典中而不是保留单个变量是否有效?在您的场景中,将变量保留在单个字典中而不是保留单个变量是否有效?