小编Sze*_*zer的帖子

不明确的扩展方法调用

此代码将无法编译:

using System;
using System.Runtime.CompilerServices;

static class Extensions {
    public static void Foo(this A a, Exception e = null, string memberName = "") {
    }

    public static void Foo<T>(this A a, T t, Exception e = null, string memberName = "")
        where T : class, IB {
    }
}

interface IB { }

class A { }

class Program {
    public static void Main() {
        var a = new A();
        var e = new Exception();

        a.Foo(e); //<- Compile error "ambiguous …
Run Code Online (Sandbox Code Playgroud)

c#

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

如何检查DbContext是否有事务?

背景:我使用SimpleInjector作为IoC的WCF服务,它为每个WCF请求创建DbContext实例.

后端本身就是CQRS.CommandHandlers有很多装饰器(验证,授权,日志记录,不同处理程序组的一些通用规则等),其中一个是Transaction Decorator:

public class TransactionCommandHandlerDecorator<TCommand> : ICommandHandler<TCommand> 
    where TCommand : ICommand
{
    private readonly ICommandHandler<TCommand> _handler;
    private readonly IMyDbContext _context;
    private readonly IPrincipal _principal;

    public TransactionCommandHandlerDecorator(ICommandHandler<TCommand> handler,
        IMyDbContext context, IPrincipal principal)
    {
        _handler = handler;
        _context = context;
        _principal = principal;
    }

    void ICommandHandler<TCommand>.Handle(TCommand command)
    {
        using (var transaction = _context.Database.BeginTransaction())
        {
            try
            {
                var user = _context.User.Single(x => x.LoginName == _principal.Identity.Name);
                _handler.Handle(command);
                _context.SaveChangesWithinExplicitTransaction(user);
                transaction.Commit();
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                throw;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

当任何命令尝试链接在同一WCF请求中执行另一个命令时,会出现问题.我在这一行得到了一个预期的例外:

using (var …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework simple-injector

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

添加git子模块作为VS中现有解决方案的参考

可以说我有子模块回购:

_git/DbModel (only source code indexed. No /bin or /obj folders)
Run Code Online (Sandbox Code Playgroud)

还有两个需要DbModel作为参考库的项目:

_git/TTT
_git/TPM
Run Code Online (Sandbox Code Playgroud)

我已经通过GitBash将DbModel作为子模块添加到它们中

 git submodule add https://RepoPath/_git/DbModel
Run Code Online (Sandbox Code Playgroud)

我的本地回购现在看起来像这样:

Source\Repos\TTT\TTT.sln
Source\Repos\TTT\DbModel\DbModel.sln

Source\Repos\TPM\TPM.sln
Source\Repos\TPM\DbModel\DbModel.sln
Run Code Online (Sandbox Code Playgroud)

我的团队资源管理器显示了现在可用的两种不同的解决方案,我可以轻松切换它们.

但.也许这是一个新手问题......

我不知道如何添加DbModel解决方案作为TPM和TTT的参考!当我在VS中单击"添加引用" - >"浏览"时,我只能添加".dll,.tlb,.olb,.ocx,.exe,.manifest"作为"参考文件".

它应该如何工作?我应该每次编译子模块解决方案并添加.dll作为参考吗?我需要添加源代码.

git projects-and-solutions visual-studio git-submodules

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

跳过然后在可空的对象上

我有一个查询,应该这样订购:

var list = new List<MonthClosureViewModel>();
var orderedList = list
    .OrderByDescending(x => x.Project)
    .ThenByDescending(x => x.ChargeLine)
    .ThenByDescending(x => x.DomesticSite) //<- x.DomesticSite might be null sometimes
    .ThenByDescending(x => x.ChargeSite)   //<- x.ChargeSite   might be null sometimes
    .ThenByDescending(x => x.RateGroup)
    .ThenByDescending(x => x.ApprovedHrs)
    .ThenByDescending(x => x.NotApprovedHrs);

public class MonthClosureViewModel
{
    public Project Project { get; set; }
    public ChargeLine ChargeLine { get; set; }
    public Site DomesticSite { get; set; }
    public Site ChargeSite { get; set; }
    public RateGroup RateGroup { get; …
Run Code Online (Sandbox Code Playgroud)

c# linq entity-framework

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

On Error Goto在EventHandler subs中不起作用

让我们假设这段代码:

模块1:

Sub main()

    Dim cl As New Class2
    On Error GoTo errorhandler1
        cl.DoWork
     On Error GoTo 0
Exit Sub

errorhandler1:
    MsgBox (Err.Description)

End Sub
Run Code Online (Sandbox Code Playgroud)

1类:

Event MyEvent()

Public Sub DoWork()
    RaiseEvent MyEvent
End Sub
Run Code Online (Sandbox Code Playgroud)

等级2:

Private WithEvents cl As Class1

Private Sub cl_MyEvent()
    Call Err.Raise(123, , "ErrorInClass")
End Sub

Private Sub Class_Initialize()
    Set cl = New Class1
End Sub

Public Sub DoWork()
    cl.DoWork
End Sub
Run Code Online (Sandbox Code Playgroud)

我希望启动errorhandler1并显示带有err.Description的MsgBox.但它反而引发了我的运行时错误.

我需要做些什么来处理EventHandlers例程中的错误?

error-handling vba

5
推荐指数
2
解决办法
231
查看次数

通用类型T作为变量名称

我正在使用EntityFramework,我需要一些方法来将实体的传入列表与数据库同步(覆盖记录描述或将它们标记为过时):

    public void Synchronize<T>(List<T> entityList)
    {
        //do something
    }
Run Code Online (Sandbox Code Playgroud)

首先,我需要从相应的表中获取所有数据并获得两个列表之间的差异(一个不可更改的东西是条目的GUID).例如,如果传入的entityList的类型是User,那么我需要从"User"表等获取所有数据.

它不起作用:

        var query = from entity in typeof(T)
                    select entity;
Run Code Online (Sandbox Code Playgroud)

当然我可以用旧的方式做到:

switch (typeof(T).Name)
        { 
            case "User":
                var query = from user in User
                            select user;
                //Sync with User table
                break;
            case "Project":
                var query = from project in Project 
                            select project ;
                //Sync with Project table
                break;
        }
Run Code Online (Sandbox Code Playgroud)

但也许有一种优雅的方式来处理通用?因为我有很多表要同步.谢谢!

c# generics entity-framework

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