小编DIl*_*d K的帖子

如何在内存中初始化测试并在每次测试中使用

我正在尝试创建单元测试.我有班级用户:

 public class User
{
    public int UsersCount
    {
        get
        {
            using (MainContext context = new MainContext())
            {
                return context.Users.Count();
            }
        }
    }
    public Guid Id { get; set; } = Guid.NewGuid();
    public string UserName { get; set; }
    public string Password { get; set; }
    public Contact UserContact { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我的第一个测试是UsersCount_Test测试,它测试UsersCount属性:

 [TestMethod]
    public void UsersCount_Test()
    {
        var user = new User();
        var context = new MainContext();
        int usersCount = context.Users.Count();
        context.Users.Add(new User());
        context.SaveChanges();
        Assert.AreEqual(usersCount + 1, …
Run Code Online (Sandbox Code Playgroud)

c# performance unit-testing

8
推荐指数
1
解决办法
298
查看次数

为什么引用属性仅通过上下文起作用

我有两个类Order和OrderDetail:

  public class Order : Entity
{
    public Order(KitchenAppContext context) : base(context)
    {

    }
    public Order() : base()
    {

    }
    public DateTime Date { get; set; }
    public Guid MenuId { get; set; }
    public virtual Menu Menu { get; set; }
    public bool IsClosed { get; set; }
    public decimal Price { get; set; }
    public virtual int PeopleCount { get { return Details.Count; } }
    public virtual List<OrderDetail> Details { get; set; } = new List<OrderDetail>();
}

 public …
Run Code Online (Sandbox Code Playgroud)

c# ef-core-2.0

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

在MEF中以通用方法转换类

我有一些类和接口:

interface IAnimal { }
interface ILiveInZoo { }
class Cat : IAnimal, ILiveInZoo { }
Run Code Online (Sandbox Code Playgroud)

另外,我有一些方法和通用方法:

class Context
{
    private static CompositionContainer Container = null;

    public ILiveInZoo GetWhoLivesInZoo(string name)
    {
        if (name == "Cat")
            return new Cat();
        return null;
    }

    public void GiveFood<T>(T animal) where T : IAnimal
    {
        var methods = Container.GetExports<Action<T, EventArgs>, AttributeMetadata>();
        //execute methods
    }
}
Run Code Online (Sandbox Code Playgroud)

这是一个用例:

Context context = new Context();
var cat = context.GetWhoLivesInZoo("Cat");
if (cat is IAnimal animal)
{
   context.GiveFood(animal);
}
Run Code Online (Sandbox Code Playgroud)

如您在GiveFood …

.net c# generics reflection mef

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

OData中的区分大小写属性时出现AmbiguousMatchException

我有两个名称相同,大小写不同的属性,Title并且TITLE

public class Product
{
    [Key]
    public Guid Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
    public string Category { get; set; }

    [NotMapped]
    public virtual string Title { get; set; }

    public string TITLE { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我在OData配置中包含Title:

ODataModelBuilder builder = new ODataConventionModelBuilder();
        builder.EntitySet<Product>("Products");
        builder.EntityType<Product>().Property(a => a.Title);
        config.MapODataServiceRoute(
        routeName: "ODataRoute",
        routePrefix: null,
        model: builder.GetEdmModel());
Run Code Online (Sandbox Code Playgroud)

这是OData控制器的操作:

  public IHttpActionResult Get(ODataQueryOptions<Product> queryOptions, CancellationToken cancellationToken)
  { …
Run Code Online (Sandbox Code Playgroud)

c# odata

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

播放时如何获取mp3的URL

我们可以从浏览器,社交网络,应用程序播放mp3文件...我想在播放mp3时获取URL。一种方法是在浏览器中创建扩展。但是在其他应用程序中我们不能。这是一个示例:我去某个站点并打开一些mp3:

mp3来自sote

如果您可以看到音乐正在播放,并且有URL。当音乐开始播放时,我想获得此URL。我怎样才能做到这一点?以及如何获取在某些应用程序中播放的音乐的URL?可能吗?

java mp3 android download

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

处理异常后发生未处理的异常

我的preveus问题中的这个主题:如何在Windows事件查看器中删除和创建日志

我创建了wpf app.我用3种方式捕捉未处理的异常:

public partial class App : Application
{
    public App()
    {
        DispatcherUnhandledException += App_DispatcherUnhandledException;
        Dispatcher.UnhandledException += Dispatcher_UnhandledException;
        AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
    }

    private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {

    }

    private void Dispatcher_UnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
    {

    }

    private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
    {

    }
}
Run Code Online (Sandbox Code Playgroud)

我正在创建这样的异常:

   public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        throw new Exception("Test exception");
    }
}
Run Code Online (Sandbox Code Playgroud)

在执行方法(Dispatcher_UnhandledException,)之后CurrentDomain_UnhandledException,App_DispatcherUnhandledException这个异常仍在抛出.Windows事件查看器正在创建这样的日志

描述:由于未处理的异常,进程终止.异常信息:System.Data.ProviderBase.DbConnectionFactory.TryGetConnection的System.InvalidOperationException(System.Data.Common.DbConnection,System.Threading.Tasks.TaskCompletionSource 1<System.Data.ProviderBase.DbConnectionInternal>, System.Data.Common.DbConnectionOptions, System.Data.ProviderBase.DbConnectionInternal, System.Data.ProviderBase.DbConnectionInternal ByRef) …

c# wpf unhandled-exception

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

从特定号码获取枚举

enum喜欢这个:

   enum Numbers
    {
        SmallerThenThree = 3,
        SmallerThenFive = 5,
        SmallerThenTen = 10,
    }
Run Code Online (Sandbox Code Playgroud)

是否有可能获得enum具体数量的项目?例如 :

 var numberFour = 4;
        var enumOfNumber = (Numbers)numberFour; // There should be SmallerThenFive enum type
        var numberSix = 6;
        enumOfNumber = (Numbers)numberSix; // There should be SmallerThenTen enum type
Run Code Online (Sandbox Code Playgroud)

c# enums

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