小编Lau*_*nce的帖子

如何将T转换为类以匹配"where T:class"约束?

在处理通用依赖注入处理程序(基本服务定位器)时,我遇到了泛型问题.

编辑1(为清晰起见)

好的,所以我实际上使用SimpleInjector作为DI解析器,它对它的GetInstance方法有类约束,所以这里有一些更完整的代码:

  public T GetInstance<T>() where T : class
  {
     try
     {
        // works
        return _container.GetInstance<T>();
     }
     catch( ActivationException aEx )
     {
        return default( T );
     }
  }

  public T GetInstance<T>()
  {
     try
     {
        if( typeof( T ).IsClass )
        {
           // does not work, T is not a reference type
           return _container.GetInstance<T>();
        }
     }
     catch( ActivationException aEx )
     {
        return default( T );
     }
  }
Run Code Online (Sandbox Code Playgroud)

编辑2 - 最终代码,因为它在评论中看起来很奇怪:

  public T GetInstance<T>()
  {
     try
     {
        if( typeof( T ).IsClass ) …
Run Code Online (Sandbox Code Playgroud)

c# generics constraints

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

Git push无法推动原点 - 没有这样的参考

我在使用远程存储库hotfix创建的分支上推送提交时遇到了问题git-flow.

这是错误:

$ git push origin hotfix/MyHotfix
Counting objects:
... etc
To {my remote repo}
 ! [remote rejected] hotfix/MyHotfix -> hotfix/MyHotfix (no such ref)
error: failed to push some refs to {my remote repo}
Run Code Online (Sandbox Code Playgroud)

hotfix用标准语法创建了:

git flow hotfix start MyHotfix
Run Code Online (Sandbox Code Playgroud)

这个分支已经存在origin,我可以看到git branch -a.我还检查了分支仍然存在于远程服务器上,因为它在我运行时出现git remote show origin.

有没有人遇到过这个git或者git-flow找到了解决方案?

注意 - 我尝试过的事情:

  • 重新克隆远程仓库 - >同样的错误
  • 删除本地分支 - >相同的错误
  • 删除远程分支 - >我可以推送'new'分支,但是当他试图推送提交时(后git remote prune origin),我的同事本地仓库会收到相同的错误 …

git hotfix git-flow

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

标签 统计

c# ×1

constraints ×1

generics ×1

git ×1

git-flow ×1

hotfix ×1