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

Sss*_*Sss 2 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;
Run Code Online (Sandbox Code Playgroud)

这个类 Node 位于另一个类中,class 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;    
} 
Run Code Online (Sandbox Code Playgroud)

现在我接下来要做的是在 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. 
       }  
} 
Run Code Online (Sandbox Code Playgroud)

有没有办法对“symbol”的数据类型进行类型转换,使其成为“processingValue”的数据类型?

我的意思是,Node Class如果我将符号的数据类型设置为"Type"或任何其他类型,然后我在构造函数中更改它的数据类型,使其与processingValue 的数据类型相同?就运行时而言,我的意思是它是一个 silverlight 应用程序,并且我有组合框可以在运行程序时从有限的数据类型(短/整型/长/Uint 之间)中进行选择,然后控件转到 Huffman 构造函数,并在中选择相应的数据类型组合框

*可以这样做吗?*非常感谢您的帮助。

aev*_*tas 5

您可以使用该dynamic类型来确保 的数据类型symbol实际上是在运行时确定的。

public dynamic symbol;
Run Code Online (Sandbox Code Playgroud)

通过这样做,以下分配将全部有效:

symbol = (long) 100;
symbol = (int) 100;
symbol = (uint) 100;
Run Code Online (Sandbox Code Playgroud)