Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.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#_Constructor_Casting_Type Conversion - Fatal编程技术网

C# 是否可以在运行时将数据类型分配给未知变量

C# 是否可以在运行时将数据类型分配给未知变量,c#,constructor,casting,type-conversion,C#,Constructor,Casting,Type Conversion,我是c silverlight-5初学者,我有一个场景,在这个场景中我使用了像这样的类节点作为结构 public class Node { public Node next, left, right; public int symbol; // This variable will create problem public int freq; }public Node front, rear; 这个类节点位于另一个类Huf

我是c silverlight-5初学者,我有一个场景,在这个场景中我使用了像这样的类节点作为结构

   public class Node
    {
        public Node next, left, right;
        public int symbol; // This variable will create problem
        public int freq;
    }public Node front, rear;
这个类节点位于另一个类Huffman中,如下所示

Class Huffman
{    
       public class Node
        {
            public Node next, left, right;
            public int symbol; // This variable will create problem
            public int freq;
        }public Node front, rear;    
} 
现在我接下来要做的是,在huffman的构造函数中,我在运行时通过另一个类的构造函数调用接收变量processingValue的数据类型。因此,processingValue的数据类型在运行时由另一个类在对Huffman的构造函数调用中决定

在哈夫曼构造器中,我必须这样做:

Class Huffman
{    
       public class Node
        {
            public Node next, left, right;
            public int symbol; // This variable will create problem
            public int freq;
        }public Node front, rear;  

      Huffman(AnotherClass object) //The call from another class is Huffman obj = new Huffman(this);
       {
        temp = new Node();
        temp.symbol = (processingValue); //THIS LINE CREATES PROBLEM  BECAUSE DATA TYPE OF "symbol" is int and may be data type of processingValue could be "short"/"long"/"UInt"etc. 
       }  
} 
是否有任何方法可以将symbol的数据类型转换为processingValue的数据类型

我的意思是在节点类中,若我将符号的数据类型设置为type或任何其他,然后在构造函数中更改它的数据类型,使其与processingValue的数据类型相同? 所谓运行时,我的意思是它是一个silverlight应用程序,在运行程序时,我可以从short/int/long/Uint中的有限数据类型中选择comboBox,然后将控件转到Huffman构造函数,并在comboBox中选择相应的数据类型

*有可能吗?*非常感谢您的帮助。

您可以使用动态类型来确保在运行时实际确定符号的数据类型

public dynamic symbol;
这样,以下作业将全部有效:

symbol = (long) 100;
symbol = (int) 100;
symbol = (uint) 100;
我会选择对象而不是动态。我只会将动力学与其他语言的复杂类型结合使用

public class Node
{
    public Node next, left, right;
    public object symbol; // This variable will create problem
    public int freq;
}

尝试使用动态类型怎么样?让我知道它是否有效。您能在运行时定义、决定吗?您的意思是您正在使用类型生成器创建一个新类型?或者您的意思是由程序员在不修改节点类的情况下使用此类型时决定。那样的话看一看泛型。@JeroenvanLangen所说的运行时,我的意思是它是一个silverlight应用程序,在运行程序时,我可以从short/int/long/Uint中的有限数据类型中选择comboBox,然后控件转到Huffman构造函数,并在comboBox中选择相应的数据类型。我需要为它包含任何库吗不要这样做,动态语言运行时DLR是从4.0版开始的.NET Framework的一部分。请注意,一次只能执行这三个赋值中的一个—您不能同时执行这三个赋值。您不能指定long and then and int。为什么不使用普通对象呢?因为我从上面读到,所选类型是SimpleType。@user234839请确保在项目中设置了这些引用。@Jeoren Little sample?我还不能理解。您可能想编辑代码示例,现在它与问题中的代码示例相同。我对您的答案进行了一些拼写编辑,可能与您的编辑冲突。很抱歉