我在尝试将通用对象添加到List <>时遇到错误.
它可能与协方差和逆变相关,但我不知道如何解决这个问题.我试图用T:IRegister来限制我的泛型类型.
我有一个接口来表示一个寄存器,然后是两个代表ByteRegister和DoubleWordRegister的类.
public interface IRegister
{
string Name {get;set;}
}
public class ByteRegister : IRegister
{
...
}
public class DoubleWordRegister : IRegister
{
...
}
Run Code Online (Sandbox Code Playgroud)
然后我有另一个类,它代表了所有相同类型的这些寄存器的块.
public class RegisterBlock<T> where T:IRegister
{
private IList<T> _registers;
... constructors, properties etc
public void AddRegister(T register)
{
_registers.Add(register);
}
}
Run Code Online (Sandbox Code Playgroud)
最后我有一个RegisterMap类,用于定义寄存器块列表和块内的每个寄存器.
public class RegisterMap
{
private List<RegisterBlock<IRegister>> _blocks;
public RegisterMap()
{
_blocks = new List<RegisterBlock<IRegister>>();
RegisterBlock<ByteRegister> block1= new RegisterBlock<ByteRegister>("Block1", 0);
block1.AddRegister(new ByteRegister("Reg1"));
block1.AddRegister(new ByteRegister("Reg2"));
_blocks.Add(block1);
RegisterBlock<DoubleWordRegister> block2= new …Run Code Online (Sandbox Code Playgroud) 我正在编写一个WPF用户控件,它显示一个包含多个页面的动态生成的TabControl,每个页面依次包含一个动态生成的控件列表(TextBox,Checkbox等).
我想根据用户是否有权查看它们来设置TextBox,CheckBox控件的可见性.此权限是每个控件ViewModel('VisiblyBy')上的值的组合,也是整个UserControl ViewModel('UserRole')的属性的组合.我刚刚开始使用WPF,但标准方法似乎是使用ValueConvertor - 但我不明白我如何编写一个可以组合/访问不同属性(VisiblyBy和UserRole)的方法,因为它们存在于不同的级别在我的ViewModel文件中.
这是我绑定到ViewModel的XAML的一部分:
<TabControl ItemsSource="{Binding VariableData.Pages}" SelectedIndex="0">
<!-- this is the header template-->
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Title}" FontWeight="Bold"/>
</DataTemplate>
</TabControl.ItemTemplate>
<!-- this is the tab content template-->
<TabControl.ContentTemplate>
<DataTemplate>
<StackPanel>
<ListBox Grid.IsSharedSizeScope="True"
ItemsSource="{Binding Variables}"
ItemTemplateSelector="{StaticResource templateSelector}">
</ListBox>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<Button Content="Cancel" />
<Button Content="Submit"
Command="{Binding DataContext.CommitChangesCommand,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type TabControl}}}" />
</StackPanel>
</StackPanel>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>Run Code Online (Sandbox Code Playgroud)
我还需要扩展控制未来可见性的变量数量,因为它还取决于使用它的应用程序的位置.
谁能为我指出以下问题的有效解决方案?
我正在处理的应用程序需要通过TCP与另一系统上运行的软件进行通信。我发送到该系统的某些请求可能需要很长时间才能完成(最多15秒)。
在我的应用程序中,我有很多线程,包括主UI线程,可以访问与远程系统通信的服务。所有线程都只能访问该服务的一个实例。
我只需要一次处理一个请求,即它需要序列化,否则TCP通讯会发生不好的事情。
到目前为止尚未尝试的解决方案
最初,我尝试将lock()与静态对象一起使用以保护每个“命令”方法,如下所示:
lock (_cmdLock)
{
SetPosition(position);
}
Run Code Online (Sandbox Code Playgroud)
但是我发现,即使远程系统和TCP通讯超时,有时它也不会释放锁。另外,如果有两个调用来自同一线程(例如,用户双击按钮),则它将越过锁定-在再次阅读有关锁定的信息后,我知道同一线程不会等待锁定。
然后,我尝试使用AutoResetEvents一次仅允许一次调用。但是如果没有锁定,它将无法与多个线程一起使用。以下是我用于发送命令(从调用线程)和处理命令请求(在其自己的线程中在后台运行)的代码
private static AutoResetEvent _cmdProcessorReadyEvent = new AutoResetEvent(false);
private static AutoResetEvent _resultAvailableEvent = new AutoResetEvent(false);
private static AutoResetEvent _sendCommandEvent = new AutoResetEvent(false);
// This method is called to send each command and can run on different threads
private bool SendCommand(Command cmd)
{
// Wait for processor thread to become ready for next cmd
if (_cmdProcessorReadyEvent.WaitOne(_timeoutSec + 500))
{
lock (_sendCmdLock)
{
_currentCommand = cmd;
}
// …Run Code Online (Sandbox Code Playgroud)