Generics 无法在c#中运行,甚至没有任何错误

我正在使用Silverlight 5(VS 2010)创建一个C#Web应用程序。 首先,我创建了控制台应用程序,因为每件事都运行良好。 但是当我在Web应用程序中执行此操作时会出现问题

即使在Web应用程序中,它也适用于特别设置的数据类型(例如,对于int而不是它工作正常)但是当我使用generics时它不起作用。 它编译无错误,但它甚至不调试设置为“切换断点”的区域。 最初GUI是这样的:

在此处输入图像描述

但是当控件传递到容易出错的部分时,GUI会突然消失

在此处输入图像描述

而且我保留断点的地方被这个替换了

在此处输入图像描述

(参见最左边的) 因此我无法调试以找到问题

我正在尝试做的一些解释:在下面的给定代码中我有一个二进制文件并存储在“fileContents”中,它是数据类型byte[] (我现在没有向您透露读取该文件的方法,你可以认为fileContents包含MainPage类中的二进制文件的内容。 实际上我会将符号(forms为0和1的符号存储在二进制文件中)并找到它的频率(通过计算它在文件中重复的次数,但没有问题所以我没有为它编写方法)。 但是我的代码中的这个processingValue变量将是generics类型( ),我将它存储在"symbol" (也是类型的(从二进制文件读取的这个符号可能是这些short / int /之一)我没有在我的代码中显示的long / UInt32 / UInt64等)。

我有这样的场景:

 using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.IO; using System.Text; using System.Runtime.InteropServices; using System.Diagnostics; namespace { public partial class MainPage: UserControl { byte[] fileContent; public MainPage() { InitializeComponent(); fileContent = null; //Suppose it already contains binary fileContents } //I press the button in order to call the Huffman class because it's Object declared in it public void ButtonClickEvent(object sender, RoutedEventArgs e) { MessageBox.Show("check0"); //Suppose i had stored contents of a file inside "fileContent" , so fileContent below contains //contents of a binary file //**THE LINE BELOW IS ERROR PRONE** Huffman  obj1 = new Huffman  (this, fileContent, BitConverter.ToUInt32); //This Object creation creates problem, whereas if i remove generic type (), Then it works fine. //If i don't use genrics then i do it like this : Huffman obj1 = new Huffman(this, fileContent); (Whereas  in Huffman class is replaced by "int" and it works fine) MessageBox.Show("check1"); //This check box is never executed } } public class Huffman  where T: struct,IComparable  ,IEquatable  { public Huffman(MainPage Object, byte[] fileContent, Func  converter) { MessageBox.Show("check2"); //It never executes length = fileContent.Length; size = Marshal.SizeOf(typeof (T)); byte[] data; for (long position = 0; position + size < length; position += size) { data = fileContent; //This data conatains the filecontents now T processingValue = converter(data, 0); { //I do something here with processingValue it could be int16/int32/int64/uin32/uint64 etc. } } } } } 

MainPage类中的Object Creation中的BitConverter函数有什么问题吗?

我甚至无法调试Huffman类,我在Huffman类的起点和终点设置了断点但是控件没有进入,并且Internet Explorer上的按钮(使用XAML GUI创建)消失了。

这是我的完整代码:(请注意我正在阅读二进制文件(任何扩展名为“.o”(FileName.o)的文件都可以用于测试我的代码,我读得很好)):

 using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.IO; using System.Text; using System.Runtime.InteropServices; using System.Diagnostics; namespace FinalSS { public partial class MainPage : UserControl { public const int CHUNK_SIZE = 4096; public const string UPLOAD_URI = "http://localhost:50323/FileUpload.ashx?filename={0}&append={1}"; public const string UPLOAD_DIALOG_FILTER = "All files (*.*)|*.*|Jpeg Images (*.jpg)|*.png|PNG Images (*.png)|*.png"; private Stream data; private string fileName; private long TotalBytes; private long UploadedBytes; byte[] fileContent; public event Action simpleEvent; public MainPage() { InitializeComponent(); progressBar.Visibility = Visibility.Collapsed; textBox.Visibility = Visibility.Collapsed; // fileContent = null; UploadedBytes = 0; TotalBytes = 0; } /* public void comboInvoke() { comboBox1.Items.Add("byte"); comboBox1.Items.Add("sbyte"); comboBox1.Items.Add("short"); comboBox1.Items.Add("int"); comboBox1.Items.Add("long"); } */ public void BrowseButtonClick(object sender, RoutedEventArgs e) { OpenFileDialog dlg = new OpenFileDialog(); dlg.Multiselect = false; dlg.Filter = UPLOAD_DIALOG_FILTER; bool? retVal = dlg.ShowDialog(); if (retVal != null && retVal == true) { progressBar.Visibility = Visibility.Visible; textBox.Visibility = Visibility.Visible; textBox.Text = "Uploading the file..."; data = dlg.File.OpenRead(); TotalBytes = data.Length; UploadedBytes = 0; fileName = dlg.File.Name; progressBar.Maximum = TotalBytes; UploadFileChunk(); } } private void UploadFileChunk() { textBox.Text = "Upload in progress..."; string uploadUri = ""; if (UploadedBytes == 0) { uploadUri = String.Format(UPLOAD_URI, fileName, 0); // Dont't append } else if (UploadedBytes < TotalBytes) { uploadUri = String.Format(UPLOAD_URI, fileName, 1); // append } else { return; // Upload finished } fileContent = new byte[CHUNK_SIZE]; int bytesRead = data.Read(fileContent, 0, CHUNK_SIZE); data.Flush(); WebClient wc = new WebClient(); wc.OpenWriteCompleted += new OpenWriteCompletedEventHandler(wc_OpenWriteCompleted); Uri u = new Uri(uploadUri); wc.OpenWriteAsync(u, null, new object[] { fileContent, bytesRead }); UploadedBytes += fileContent.Length; MessageBox.Show("check0"); Huffman obj1 = new Huffman(this, fileContent, BitConverter.ToUInt32); MessageBox.Show("check1"); } void wc_OpenWriteCompleted(object sender, OpenWriteCompletedEventArgs e) { progressBar.Value = UploadedBytes; if (e.Error == null) { object[] objArr = e.UserState as object[]; byte[] fileContent = objArr[0] as byte[]; int bytesRead = Convert.ToInt32(objArr[1]); Stream outputStream = e.Result; outputStream.Write(fileContent, 0, bytesRead); outputStream.Close(); if (UploadedBytes < TotalBytes) { UploadFileChunk(); } else { textBox.Text = fileName; } } } ///  /// /////////////////////////////////////////////////////// ///  ///  ///  private void ShowButtonClick(object sender, RoutedEventArgs e) { if (simpleEvent != null) simpleEvent(); } private void CompressButtonClick(object sender, RoutedEventArgs e) { } private void CloseButtonClick(object sender, RoutedEventArgs e) { } private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e) { } private void textBox_TextChanged(object sender, TextChangedEventArgs e) { } private void TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs e) { } private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e) { } private void UserControl_Loaded(object sender, RoutedEventArgs e) { } private void listBox2_SelectionChanged(object sender, SelectionChangedEventArgs e) { } private void TreeViewItem_Selected_1(object sender, RoutedEventArgs e) { } private void TreeViewItem_Selected(object sender, RoutedEventArgs e) { } } public class Huffman where T : struct, IComparable, IEquatable { long length; int size; byte[] data; public class Node { public Node next, left, right; public T symbol; // This symbol is of generic type. public int freq; public int is_processed; } public Node front, rear; public Huffman(MainPage form, byte[] fileContent,Func  converter) { MessageBox.Show("check2"); // form.simpleEvent += () => ShowClick(form,fileContent); length = 0; front = null; rear = null; MessageBox.Show("check4"); length = fileContent.Length; size = Marshal.SizeOf(typeof(T)); Stream stream = new MemoryStream(fileContent); { for (long position = 0; position + size < length; position += size) { data = fileContent; T processingValue = converter(data, 0); { Node pt, temp; bool is_there = false; pt = front; while (pt != null) { form.listBox1.Visibility = Visibility.Visible; if (pt.symbol.Equals(processingValue)) { pt.freq++; is_there = true; break; } temp = pt; pt = pt.next; } if (is_there == false) { temp = new Node(); temp.symbol = processingValue; temp.freq = 1; temp.left = null; temp.right = null; temp.next = null; temp.is_processed = 0; if (front == null) { front = temp; } else { temp.next = front; front = temp; } } } } stream.Close(); } MessageBox.Show("Yes correctly done"); merge_sort(front); Print_tree(front, form); } public Node merge_sort(Node head) { if (head == null || head.next == null) { return head; } Node middle = getMiddle(head); Node sHalf = middle.next; middle.next = null; return merge(merge_sort(head), merge_sort(sHalf)); } public Node merge(Node a, Node b) { Node dummyHead, curr; dummyHead = new Node(); curr = dummyHead; while (a != null && b != null) { if (a.freq <= b.freq) { curr.next = a; a = a.next; } else { curr.next = b; b = b.next; } curr = curr.next; } curr.next = (a == null) ? b : a; return dummyHead.next; } public Node getMiddle(Node head) { if (head == null) { return head; } Node slow, fast; slow = fast = head; while (fast.next != null && fast.next.next != null) { slow = slow.next; fast = fast.next.next; } return slow; } /////// public void Print_tree(Node treee,MainPage obj) { Node pt = treee; while (pt != null) { obj.listBox1.Items.Add("Symbol :" + pt.symbol + " -" + " Frequency : " + pt.freq); // Debug.WriteLine("Symbol :" + pt.symbol + " -" + " Frequency : " + pt.freq); pt = pt.next; } } } } 

这是xml代码:

                                                       

就我的担保而言,这个问题是由于Huffman构造函数中的BitConverter调用函数Func转换器产生的问题。 我想我需要以其他方式使用Func转换器

在您看来,我看不到您的代码有任何错误。在Silverlight中,您无法使用断点进行Console或WinForm应用程序的调试。 你能发送你的解决方案吗? 然后我可以看看。