小编fhn*_*eer的帖子

模拟索引属性

我正在使用Moq编写单元测试.我创建了一个模拟对象.现在,当我尝试模拟其属性时,我收到错误"表达式树可能不包含索引属性"

这是我的代码.

public Node GetNode(IMyInterface interface, string itemName)
{
    return interface.Items[itemName];
}
Run Code Online (Sandbox Code Playgroud)

这是单元测试

var expected = new Node();
var itemName = "TestName";
var mock = new Mock<IMyInterface>();
mock.Setup(f => f.Items[itemName]).Returns(expected);
var target = new MyClass();

var actual = target.GetNode(mock.Object, itemName);
Assert.AreEqual(expected, actual);
Run Code Online (Sandbox Code Playgroud)

这条线给了我错误.

mock.Setup(f => f.Items[itemName]).Returns(expected);
Run Code Online (Sandbox Code Playgroud)

我怎么能这个功能.

c# unit-testing moq

10
推荐指数
1
解决办法
1102
查看次数

附加到进程时,Visual Studio不会加载模块

我有一个C++应用程序.当我在Visual Studio应用程序启动时按F5,我可以调试它.但是,当我从Windows资源管理器运行应用程序,然后在visual studio中附加此过程时,我看到断点可以被击中(它们完全是红色的)但是断点没有被击中.当我看到模块窗口时,那里没有任何东西.有什么问题?

c++ debugging visual-studio-2010

10
推荐指数
1
解决办法
1万
查看次数

Visual Studio C++多行注释

在VS C++代码中,如果我没有选择任何内容或选择完整行并按下注释选择(Ctrl + K + Ctrl + C),那么它将使用//注释整行

int x = 5;
Run Code Online (Sandbox Code Playgroud)

按Ctrl + K + Ctrl + C后不选择任何内容或选择全行.

// int x = 5;
Run Code Online (Sandbox Code Playgroud)

现在,如果我选择该行的某些部分并再次按下注释按钮,则仅评论所选文本(粗体表示已选中)

int x = 5 ;

按下Ctrl + K + Ctrl + C并选择x = 5.

int /*x = 5*/;
Run Code Online (Sandbox Code Playgroud)

包含多条线

int x = 5;

int y = 2;

int z = x*5;

评论后快捷方式

int/* x = 5;
int y = 2;
int z =*/ x * 5;
Run Code Online (Sandbox Code Playgroud)

我想要的是

//int x = 5;
//int y = 2; …
Run Code Online (Sandbox Code Playgroud)

c++ comments styling visual-studio-2010

10
推荐指数
1
解决办法
1万
查看次数

使用MVVM重置组合框中的组合框选定项目

我在我的WPF应用程序中使用ComboBox并跟随MVVM.我想在ComboBox中显示一个字符串列表.

XAML:

<ComboBox ItemsSource="{Binding ItemsCollection}" SelectedItem="{Binding SelectedItem}" />
Run Code Online (Sandbox Code Playgroud)

查看型号:

public Collection<string> ItemsCollection; // Suppose this has 10 values.
private string _selectedItem;
public string SelectedItem
{
    get { return _selectedItem; }
    set
    {
        _selectedItem = value;
        Trigger Notify of property changed.
    }
}
Run Code Online (Sandbox Code Playgroud)

现在这段代码工作得很好.我可以从视图中进行选择,我可以在ViewModel中进行更改,如果我从ViewModel更改SelectedItem,我可以在我的视图中看到它.

现在这就是我想要实现的目标.当我从我的视图中更改所选项目时,我需要检查值是好/坏(或任何)设置所选项目,否则不设置它.所以我的视图模型就像这样改变了.

public string SelectedItem
{
    get { return _selectedItem; }
    set
    {
        if (SomeCondition(value))
            _selectedItem = value;           // Update selected item.
        else
            _selectedItem = _selectedItem;   // Do not update selected item.
        Trigger Notify of property changed.
    } …
Run Code Online (Sandbox Code Playgroud)

c# wpf combobox selecteditem mvvm

9
推荐指数
2
解决办法
9872
查看次数

实体类型[名称]不是当前上下文的模型的一部分

我使用EF创建模型并使用DbContext 5.X生成器生成其上下文.现在我重命名了我的一个实体的类名.现在当我运行我的代码时,我得到"实体类型Student2不是当前上下文模型的一部分." 错误.

var context = new MyEntities(connectionString);
foreach(var student in context.Students)
{
    Console.WriteLine(class.Name.ToString());
}
Run Code Online (Sandbox Code Playgroud)

在我的数据上下文中.

public partial class MyEntities : DbContext
{
    public MyEntities()
        : base("name=MyEntities")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

    // public DbSet<Student> Students { get; set; } -> Origional
    public DbSet<Student2> Student { get; set; } // I renamed Student to Student2
}
Run Code Online (Sandbox Code Playgroud)

如何解决这个问题?由于一些冲突,我需要重命名我的班级.

c# poco database-first entity-framework-5

8
推荐指数
2
解决办法
2万
查看次数

在mef中卸载dll文件

我有一些插件作为DLL文件.我的应用程序加载DLL并运行正常.但是当我尝试删除旧插件并用新插件替换它时,它不允许我这样做.因为它已被应用程序加载.我发现通过使用appdomain,我们可以做到这一点.但我无法找到使用mef的解决方案.

我需要一个可以在mef上运行的代码.下面是我的代码,用于加载插件.

//Creating an instance of aggregate catalog. It aggregates other catalogs
var aggregateCatalog = new AggregateCatalog();

//Build the directory path where the parts will be available
var directoryPath = "Path to plugins folder";

//Load parts from the available dlls in the specified path using the directory catalog
var directoryCatalog = new DirectoryCatalog(directoryPath, "*.dll");

//Add to the aggregate catalog
aggregateCatalog.Catalogs.Add(directoryCatalog);

//Crete the composition container
var container = new CompositionContainer(aggregateCatalog);


// Composable parts are created here i.e. the Import and Export …
Run Code Online (Sandbox Code Playgroud)

c# dll mef

7
推荐指数
1
解决办法
3965
查看次数

ListBox selectedItem工作但设置不在MVVM中工作

我正在研究WPF应用程序并关注MVVM.在我看来,有一个包含不同列的网格视图.其中一列是ListBox.现在的问题是,对于ListBox列,SelectedItem get工作正常但set没有.

这是我的View代码

<DataGrid ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}" SelectionMode="Single">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Name}" Header="Name" />
        <DataGridTemplateColumn Header="Actions">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ListBox DisplayMemberPath="Name" ItemsSource="{Binding Actions}" SelectedItem="{Binding SelectedAction}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>
Run Code Online (Sandbox Code Playgroud)

在我的ViewModel中,我有Main ViewModel类,其中包含Items列表.Item类包含name,操作列表和所选操作.

public class MyViewModel : INotifyOfPropertyChanged
{
    private ObservableCollection<Item> _items;
    public ObservableCollection<Item> Items
    {
        get { return _items?? (_items= new ObservableCollection<Item>); }
    }

    private Item _selectedItem;
    public Item SelectedItem
    {
        get { return _selectedItem; }
        set { _selectedItem= value; }
    }
}

public class Item : INotifyOfPropertyChanged
{ …
Run Code Online (Sandbox Code Playgroud)

c# wpf listbox mvvm

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

可访问性不一致:基类比子类更难访问

我正在读约瑟夫·阿尔巴巴里和本·阿尔巴巴里的书"简而言之的C#4.0".从那里我发现访问修饰符的主题限制.第91页,主题"访问修饰符的限制".

从书中引用.

编译器会阻止任何不一致的访问修饰符的使用.例如,子类本身可以比基类更难访问,但不能更多

所以这说明基类应该与子类相同或更易于访问.因此,如果基类是内部的,那么子类应该是私有的或内部的.如果基类是私有的,而子类是公共的,那么将生成编译时错误.在Visual Studio中尝试这个时,我发现了一些奇怪的行为.

尝试1:Base是私有的,子类是私有的(Works,正确的行为)如果两者都是内部的,那么它也有效.

private class A { }
private class B : A { }         // Works
Run Code Online (Sandbox Code Playgroud)

尝试2:Base是私有的,子类是public或internal(这是失败的,正确的行为)

private class A { }
public class B : A { }          // Error
Run Code Online (Sandbox Code Playgroud)

尝试3:Base是内部的,sub是公共的(这是有效的,但它应该失败.因为Base比子类更难访问

internal class A { }
public class B : A { }          // Works, but why
Run Code Online (Sandbox Code Playgroud)

现在我的问题是为什么试试3没有失败?子类是公共的,比内部的基类更容易访问.即使这本书说这应该失败.但Visual Studio成功编译了这个.这应该工作与否?

编辑:

我在VS中创建了一个新的控制台项目.在Program.cs中,我添加了我的代码.这是Program.cs文件的完整代码.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication
{
    class Program
    {
        internal class A { }
        public class B …
Run Code Online (Sandbox Code Playgroud)

c# inheritance base-class

7
推荐指数
1
解决办法
9429
查看次数

我应该使用什么作为返回类型的方法IEnumerable,IList,Collection或者什么

我正在创建一个将被不同应用程序广泛使用的库.您可以说它是一种公共库或SDK.

目前我正在开发一个函数,它获取点列表对这些点执行一些计算,然后返回更新点列表.所以我的问题是我应该使用什么作为返回类型和我的参数.IList,IEnumerableCollection.

所以这里是功能.我不知道用户将对输出做什么.用户如何使用它,就在他身上.那么什么应该是在这里使用的最佳选择.

public static IEnumerable<Point2D> PeformSomeCalculation(IEnumerable<Point2D> points)
{
    // Do something,
    return updatedPoints;
}
Run Code Online (Sandbox Code Playgroud)

.net c# collections ienumerable ilist

6
推荐指数
1
解决办法
1190
查看次数

Visual Studio:禁止对命名空间中的所有文件发出警告

我的项目中有以下命名空间。

在此处输入图片说明

我想禁用特定命名空间上的特定警告(比如 Project.ViewModels)。我可以通过在GlobalSuppression.cs

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Formatting", "RCS1057:Add empty line between declarations.", Justification = "<Pending>", Scope = "type", Target = "~T:Project.ViewModels.MainViewModel.cs")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Formatting", "RCS1057:Add empty line between declarations.", Justification = "<Pending>", Scope = "type", Target = "~T:Project.ViewModels.TreeViewModel.cs")]
Run Code Online (Sandbox Code Playgroud)

我试图Scopetype改为namespacenamespaceanddescendants但没有奏效。

[assembly: SuppressMessage("Formatting", "RCS1057:Add empty line between declarations.", Justification = "<Pending>", Scope = "namespace", Target = "~T:Project.ViewModels")]
Run Code Online (Sandbox Code Playgroud)

知道如何解决这个问题吗?我正在使用 Visual Studio 2017。

c# code-analysis visual-studio roslyn roslyn-code-analysis

6
推荐指数
1
解决办法
2172
查看次数