小编ket*_*ket的帖子

如何使用 MongoDb 的自定义值类型序列化字典?

我有以下类,我想写入 MongoDb 的实例:

public class ChartDefinitionBase
{
    public ChartDefinitionBase(ObjectId id, IDictionary<ISite, IExample> examples)
    {
        ObjectId = id;
        Examples = examples;
    }

    [BsonIgnoreIfDefault]
    [BsonId(IdGenerator = typeof(ObjectIdGenerator))]
    public ObjectId ObjectId { get; private set; }

    [BsonDictionaryOptions(DictionaryRepresentation.ArrayOfDocuments)]
    public IDictionary<ISite, IExample> Examples{ get; private set; }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试InsertOne()在这种类型的实例上调用 ' ' 时,我得到以下信息ArgumentException

GenericArguments[1], 'System.Collections.Generic.List{1}[System.Collections.Generic.KeyValuePair{2}[System.String,System.Object]]',在 'MongoDB.Bson.Serialization.Serializers.ImpliedImplementationInterfaceSerializer {2}[TInterface,TImplementation]' 违反了类型 'TImplementation' 的约束。

例如,如果我将字典值类型转换为字符串,异常就会消失并且序列化效果很好。但是,如果我使用 的具体实现,则会IExample得到相同的异常。
如何获取具有自定义值类型的字典以进行序列化?

c# serialization dictionary mongodb

5
推荐指数
0
解决办法
869
查看次数

对于包含UserControl的项目,Dotnet构建失败(InitializeComponent在当前上下文中不存在)

我有一个使用.NET Core格式定义的.csproj项目。该项目包含由.xaml文件和关联的.cs文件组成的用户控制文件。以前,在这些用户控件上出现编译错误,直到将以下内容添加到.csproj文件中:

  <ItemGroup>
    <Page Include="**\*.xaml">
      <SubType>Designer</SubType>
      <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
    </Page>
    <Compile Update="**\*.xaml.cs" SubType="Code" DependentUpon="%(Filename)" />
  </ItemGroup>
Run Code Online (Sandbox Code Playgroud)

那时,解决方案开始在Visual Studio中编译。但是,当我尝试使用dotnet build从命令行进行构建时,构建失败,并出现了我以前在Visual Studio中看到的错误:The name 'InitializeComponent' does not exist in the current context.

有什么想法为什么解决方案可以在VS2017中构建但不使用dotnet构建,或者如何解决这个问题?

编辑:不带PackageReferences的.csproj内容

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <LanguageTargets>$(MSBuildToolsPath)\Microsoft.CSharp.targets</LanguageTargets>
    <TargetFramework>net45</TargetFramework>
    <IsPackable>true</IsPackable>
  </PropertyGroup>

  <ItemGroup>
    <Page Include="**\*.xaml">
      <SubType>Designer</SubType>
      <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
    </Page>
    <Compile Update="**\*.xaml.cs" SubType="Code" DependentUpon="%(Filename)" />
  </ItemGroup>

  <ItemGroup>
    <Reference Include="PresentationCore" />
    <Reference Include="PresentationFramework" />
    <Reference Include="System" />
    <Reference Include="System.ComponentModel.DataAnnotations" />
    <Reference Include="System.Core" />
    <Reference Include="System.Xaml" />
    <Reference Include="WindowsBase" />
  </ItemGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)

c# msbuild wpf build visual-studio

5
推荐指数
1
解决办法
638
查看次数

为什么 WhenAnyValue observable 在订阅时触发?

我有以下虚拟视图模型:

public class DummyViewModel : ReactiveObject
{
    internal DummyViewModel()
    {
        ItemChanged.Subscribe(_ => Console.WriteLine());
    }

    public IObservable<string> ItemChanged
    {
        get { return this.WhenAnyValue(x => x.Item).Select(s => s); }
    }

    private string _item;

    public string Item
    {
        get { return _item; }
        set { this.RaiseAndSetIfChanged(ref _item, value); }
    }
}
Run Code Online (Sandbox Code Playgroud)

当我创建这个类的一个新实例时,observable 立即在订阅时触发,返回 null(没有任何东西绑定到 Item)。这在我更高级的视图模型中导致了一个问题,我有多个 observable 需要以不同的方式链接在一起。我一直在使用 Skip 和 StartWith 的组合,但它变得非常复杂。有人可以建议为什么会发生这种情况以及我是否应该考虑采用不同的方法?

c# mvvm reactive-programming system.reactive reactiveui

2
推荐指数
1
解决办法
1098
查看次数

当依赖属性更改时,不会引发WPF事件

我有以下自定义控件基于链接的"重选项" :

public partial class SelectableContentControl : ContentControl
{
    public SelectableContentControl()
    {
        InitializeComponent();
        var isCheckedDesc = DependencyPropertyDescriptor.FromProperty(IsCheckedProperty, typeof(SelectableContentControl));
        isCheckedDesc.AddValueChanged(this, IsCheckedPropertyChanged);
    }

    public bool IsChecked
    {
        get { return (bool)GetValue(IsCheckedProperty); }
        set { SetValue(IsCheckedProperty, value); }
    }

    public static readonly DependencyProperty IsCheckedProperty =
        DependencyProperty.Register("IsChecked", typeof(bool),
          typeof(SelectableContentControl), new PropertyMetadata(false));

    private void IsCheckedPropertyChanged(object sender, EventArgs e)
    {
        var selectable = Content as IAmSelectable;
        if (selectable != null) selectable.IsSelected = IsChecked;
    }
}
Run Code Online (Sandbox Code Playgroud)

为此定义的样式SelectableContentControl如下:

<Style TargetType="{x:Type controls1:SelectableContentControl}">
    <Setter Property="Template">
        <Setter.Value> …
Run Code Online (Sandbox Code Playgroud)

c# wpf xaml controls dependency-properties

2
推荐指数
1
解决办法
357
查看次数