仿制药<;T>;不在c#中工作,甚至运行时没有任何错误
我正在使用Silverlight 5(VS2010)创建一个C#web应用程序。首先,我创建了控制台应用程序,因为一切都很好。但当我在web应用程序中这样做时,问题就出现了 即使在web应用程序中,对于特定的数据类型,它也可以正常工作(例如,对于仿制药<;T>;不在c#中工作,甚至运行时没有任何错误,c#,generics,web-applications,silverlight-5.0,C#,Generics,Web Applications,Silverlight 5.0,我正在使用Silverlight 5(VS2010)创建一个C#web应用程序。首先,我创建了控制台应用程序,因为一切都很好。但当我在web应用程序中这样做时,问题就出现了 即使在web应用程序中,对于特定的数据类型,它也可以正常工作(例如,对于int而不是,它可以正常工作),但当我使用泛型时,它就不起作用了它编译时无错误,但它甚至不调试设置为“切换断点”的区域。最初,GUI是这样的: using System; using System.Collections.Generic; using S
int
而不是
,它可以正常工作),但当我使用泛型时,它就不起作用了它编译时无错误,但它甚至不调试设置为“切换断点”的区域。最初,GUI是这样的:
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 < uint > obj1 = new Huffman < uint > (this, fileContent, BitConverter.ToUInt32);
//This Object creation creates problem, whereas if i remove generic type (<T>), Then it works fine.
//If i don't use genrics then i do it like this : Huffman obj1 = new Huffman(this, fileContent); (Whereas <T> in Huffman class is replaced by "int" and it works fine)
MessageBox.Show("check1"); //This check box is never executed
}
}
public class Huffman < T > where T: struct,IComparable < T > ,IEquatable < T >
{
public Huffman(MainPage Object, byte[] fileContent, Func < byte[], int, T > 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.
}
}
}
}
}
但是,当控件传递到容易出错的部分时,GUI会像这样突然消失
我保存断点的地方被这个替换了
(请参见最左端)因此,我无法调试以找到问题
解释一下我想做什么:在下面给定的代码中,我有一个二进制文件,存储在数据类型为byte[]的“fileContents”中。
(我并没有向您透露读取该文件的方法,现在您可以考虑文件内容包含“代码>主页processingValue
变量将是泛型(
),我将存储在“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 < uint > obj1 = new Huffman < uint > (this, fileContent, BitConverter.ToUInt32);
//This Object creation creates problem, whereas if i remove generic type (<T>), Then it works fine.
//If i don't use genrics then i do it like this : Huffman obj1 = new Huffman(this, fileContent); (Whereas <T> in Huffman class is replaced by "int" and it works fine)
MessageBox.Show("check1"); //This check box is never executed
}
}
public class Huffman < T > where T: struct,IComparable < T > ,IEquatable < T >
{
public Huffman(MainPage Object, byte[] fileContent, Func < byte[], int, T > 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.
}
}
}
}
}
以下是xml代码:
<UserControl x:Class="FinalSS.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
d:DesignHeight="300" d:DesignWidth="400" Loaded="UserControl_Loaded">
<Grid x:Name="LayoutRoot" Background="Wheat" Visibility="Visible" Height="348" Width="681">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="37*" />
<ColumnDefinition Width="86*" />
<ColumnDefinition Width="558*" />
</Grid.ColumnDefinitions>
<sdk:TreeView SelectedItemChanged="TreeView_SelectedItemChanged" Margin="12,12,12,41" Background="wheat" Grid.ColumnSpan="3">
<sdk:TreeViewItem Header="File" Selected="TreeViewItem_Selected" >
<sdk:TreeViewItem.Items>
<sdk:TreeViewItem Selected="TreeViewItem_Selected_1">
<sdk:TreeViewItem.Header>
<StackPanel Orientation="Horizontal" >
<Button Content="Browse File" Width="76" Height="20" Click="BrowseButtonClick"></Button>
</StackPanel>
</sdk:TreeViewItem.Header>
</sdk:TreeViewItem>
<sdk:TreeViewItem Selected="TreeViewItem_Selected_1">
<sdk:TreeViewItem.Header>
<StackPanel Orientation="Horizontal">
<Button Content="Show Data" Width="75" Height="20" Click="ShowButtonClick"></Button>
</StackPanel>
</sdk:TreeViewItem.Header>
</sdk:TreeViewItem>
<sdk:TreeViewItem Selected="TreeViewItem_Selected_1">
<sdk:TreeViewItem.Header>
<StackPanel Orientation="Horizontal">
<Button Content="Compress" Width="75" Height="20" Click="CompressButtonClick"></Button>
</StackPanel>
</sdk:TreeViewItem.Header>
</sdk:TreeViewItem>
<sdk:TreeViewItem Selected="TreeViewItem_Selected_1">
<sdk:TreeViewItem.Header>
<StackPanel Orientation="Horizontal">
<Button Content="close" Width="75" Height="20" Click="CloseButtonClick" ></Button>
</StackPanel>
</sdk:TreeViewItem.Header>
</sdk:TreeViewItem>
</sdk:TreeViewItem.Items>
</sdk:TreeViewItem>
</sdk:TreeView>
<ProgressBar Name="progressBar" Height="9" HorizontalAlignment="Left" Margin="216,82,0,0" VerticalAlignment="Top" Width="139" Foreground="#FF3AB802" Grid.Column="2" Visibility="Collapsed" />
<TextBox Name="textBox" Height="23" HorizontalAlignment="Left" Margin="146,68,0,0" VerticalAlignment="Top" Width="66" Grid.Column="2" TextChanged="textBox_TextChanged" Visibility="Collapsed" />
<ListBox Height="148" HorizontalAlignment="Left" Margin="0,152,0,0" Name="listBox1" VerticalAlignment="Top" Width="197" ItemsSource="{Binding}" Visibility="Collapsed" Grid.Column="2" SelectionChanged="listBox1_SelectionChanged"></ListBox>
<ListBox Height="148" HorizontalAlignment="Right" Margin="0,154,160,0" Name="listBox2" VerticalAlignment="Top" Width="203" SelectionChanged="listBox2_SelectionChanged" Visibility="Collapsed" Grid.Column="2">
<ListBoxItem />
<ListBoxItem />
</ListBox>
<ComboBox Height="19" HorizontalAlignment="Left" Margin="13,204,0,0" Name="comboBox1" VerticalAlignment="Top" Width="104" SelectionChanged="comboBox1_SelectionChanged" Grid.ColumnSpan="2">
<ComboBoxItem />
<ComboBoxItem />
</ComboBox>
</Grid>
</UserControl>
就我的担保人而言,这个问题是由于哈夫曼构造函数中的位转换器调用函数Func converter造成的问题。我想我需要以其他方式使用Func converter在你看来,我看不到你的代码有任何错误。在silverlight中,你不能像Console或WinForm App那样调试使用断点进行复制。
您能发送您的解决方案吗?然后我可以查看。@Zong谢谢您的编辑,还有一个问题,您能告诉我您是如何在其中添加屏幕截图的吗?步骤是什么?(在回答问题之前,这样我就能够高质量地编写问题)@ZongZhengLi谢谢,好的,我以后会处理的。但是你能在代码中找到问题吗,如果可以,请告诉我解决方法,如果可以的话?非常感谢。值得一提的是,你的断点没有设置在合法的行上。(该行没有执行代码)@KirkWoll好的,谢谢你的回复,但是它应该执行Huffman类,即使我已经为它创建了对象,所以它必须调用,但是为什么它不这样做呢?谢谢,我已经添加了这两个代码,请查看编辑部分。将你的解决方案发送到harshanamcrox10@gmail.com.然后我可以查一下这里有什么问题。好的,一个汉克斯大哥……你会得到的5分钟后在你的邮件里。