小编lab*_*roo的帖子

Action(arg)和Action.Invoke(arg)之间的区别

static void Main()
{
    Action<string> myAction = SomeMethod;

    myAction("Hello World");
    myAction.Invoke("Hello World");
}

static void SomeMethod(string someString)
{
    Console.WriteLine(someString);
}
Run Code Online (Sandbox Code Playgroud)

上面的输出是:

Hello World
Hello World
Run Code Online (Sandbox Code Playgroud)

现在我的问题是

  • 如果有的话,调用Action的两种方法有什么区别?

  • 这个比那个好吗?

  • 何时使用哪个?

谢谢

c#

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

Await或Task.FromResult

我有一个服务可以说,

public interface ISomeService
{
    Task<bool> DoSomeExpensiveCheckAsync(string parameter);
}
Run Code Online (Sandbox Code Playgroud)

我有这个课程来消费服务.它只需要做一些简单的空检查,然后返回服务响应.

public class SomeServiceConsumer
{
    private readonly ISomeService _serviceClient;

    public SomeServiceConsumer(ISomeService serviceClient)
    {
        _serviceClient = serviceClient;
    }

    public async Task<bool> DoSomething1Async(string someParameter)
    {
        if (string.IsNullOrWhiteSpace(someParameter))
        {
            return false;
        }
        return await _serviceClient.DoSomeExpensiveCheckAsync(someParameter);
    }

    //No async or await keywords   
    public Task<bool> DoSomething2Async(string someParameter)
    {
        if (string.IsNullOrWhiteSpace(someParameter))
        {
            return Task.FromResult(false);
        }
        return _serviceClient.DoSomeExpensiveCheckAsync(someParameter);
    }
}
Run Code Online (Sandbox Code Playgroud)

我应该做的DoSomething1Async还是DoSomething2Async

根据这个答案,我不应该用不必要的东西包裹await但是我必须Task.FromResult(false)用于短路DoSomething2Async

但根据这个答案,有一些案例try/catch和 …

.net c# task-parallel-library async-await

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

指定内容的构建操作 - Nuget

告诉Nuget包将所有css文件添加为嵌入式资源(即构建操作是嵌入式资源)的最简单方法是什么.

我试图通过工具文件夹中的install.ps1来做到这一点,但仍然无法到达任何地方

注意:我从目录结构创建包(tools\content\lib)

这是我的install.ps1,它不起作用.

param($installPath, $toolsPath, $package, $project)
$MsbNS = @{msb = 'http://schemas.microsoft.com/developer/msbuild/2003'}    
function EmbeddContent($ProjectLink, [string]$XPath)
{
    $nodes = @(Select-Xml $XPath $ProjectLink -Namespace $MsbNS | Foreach {$_.Node})

    foreach ($node in $nodes)
    {   
    if($node.Include.StartsWith("Content\css"))
    {           
        $cet = $node.ownerdocument.CreateElement("EmbeddedResource")
        $cet.setAttribute("Include", $node.Include)
        $parent = $node.ParentNode          
        [void]$parent.RemoveChild($node)
        [void]$parent.Appendchild($cet)        
    }
    }
}
$project.Save()
$fileLocation = $project.FileName
$dte.ExecuteCommand("Project.UnloadProject");

$proj = [xml](gc $fileLocation)
Embeddcontent $fileLocation '//msb:Project/msb:ItemGroup/msb:Content'
$proj.Save($fileLocation)
Run Code Online (Sandbox Code Playgroud)

请帮忙 ..

c# powershell embedded-resource visual-studio nuget

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

使用Html和C#的Metro风格应用

根据这张幻灯片

http://media.infragistics.com/community/general/windows8-platform-tools.jpg

这是否意味着如果我想开发一个Metro风格的应用程序,我必须使用C#的XAML视图?

我可以使用HTML/JS/CSS - C#与事件处理程序的组合吗?像ASP.NET Webforms/MVC之类的东西.我知道它不是相同的客户端服务器架构,但由于metro风格的应用程序支持HTML/JS,我很想知道.

我可以使用Win-JS.但是我可以比Javascript编写C#,使用HTML而不是XAML吗?(我不知道XAML和我喜欢C#)

我在网上找到的所有C#样本都使用XAML.

c# xaml microsoft-metro windows-8

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

使用带有IdentityFactory中间件的IOC容器

我试图理解UserManagerFactory中间件,这里解释了usermanager的每个请求生命周期管理.

我创建了这个我从Startup Configuration方法调用的类

public class CustomUserManagerProvider
{
    public static CustomUserStore<CustomUser> CreateCustomUserStore()
    {
        return new CustomUserStore<CustomUser>(/*Need to inject dependencies here*/);
    }

    public static CustomUserManager CreateCustomUserManager(IdentityFactoryOptions<CustomUserManager> options,
        IOwinContext context)
    {
        return new CustomUserManager(context.Get<CustomUserStore<CustomUser>>());
    }
}
Run Code Online (Sandbox Code Playgroud)

和启动配置

public void Configuration(IAppBuilder app)
    {
        app.CreatePerOwinContext(CustomUserManagerProvider.CreateCustomUserStore);
        app.CreatePerOwinContext<IngramUserManager>(CustomUserManagerProvider.CreateCustomUserManager);
        ////....Other things
    }
Run Code Online (Sandbox Code Playgroud)

现在,我的CustomUserStore有一些依赖项,我想在构造函数中注入.

IOC容器的组合根知道如何解决这些依赖关系.

如何CustomUserManagerProvider识别DI容器(如果有意义的话)......

虽然这有效

public static CustomUserStore<CustomUser> CreateCustomUserStore()
    {
        var dependency = DependencyResolver.Current.GetService<ISomeDependency>();          
        return new CustomUserStore<CustomUser>(dependency);
    }
Run Code Online (Sandbox Code Playgroud)

但是,我试图避免服务定位器(反)模式.这是我唯一的选择吗,这是对的吗?

我正在使用Ninject.

我只是在组合根中的requestScope中创建一个UserManager并注入到控制器中,这不是同一个东西吗?

在Web应用程序中,CreatePerOwinContext与创建InRequestScope相同吗?

c# asp.net-identity

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

Moq无法在构造函数中使用可为null的参数创建类的模拟对象

我有这门课:

public class TestClass
{
 public TestClass(int? foo, string bar)
 {
   //..Something
 }
}
Run Code Online (Sandbox Code Playgroud)

我试图用这样的MOQ来模拟它

var mockA = new Mock<A>(new object[] {(int?)1, string.Empty})
Run Code Online (Sandbox Code Playgroud)

或这个,

var mockA = new Mock<A>(new object[] {1 as int?, string.Empty})
Run Code Online (Sandbox Code Playgroud)

即使这样

var mockA = new Mock<A>(new object[] {(int?)null, string.Empty})
Run Code Online (Sandbox Code Playgroud)

当我试图得到这样的对象

var objA = mockA.Object
Run Code Online (Sandbox Code Playgroud)

它抛出了这个例外

无法实例化类的代理:TestClass.找不到与给定参数匹配的构造函数:System.Int32,System.String

(对于第三个,它抛出空引用异常)

如何使它识别第一个参数是Nullable System.Int32类型而不是System.Int32.

(请忽略我正在嘲笑一个类,而不是一个接口.)

c# unit-testing nullable moq mocking

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

"on"不绑定到动态添加的元素

我的HTML

<div>    
    <span  class="more-available" data-completeMessage="This is the complete message you see after clicking more">Hello</span>?
</div>
Run Code Online (Sandbox Code Playgroud)

我动态地在末尾添加一个锚标记,然后想要将一个点击处理程序附加到锚标记.所以我这样做

$(document).ready(function() {

   //Attach future proof click event to an anchor tag
   $('a.more').on('click', function() {
      var $parent = $(this).parent();
      $parent.text($parent.data('completemessage'));
   });

   //Add the anchor tag
   $('span.more-available').append($('<a class="more">...more</a>'));
});;?
Run Code Online (Sandbox Code Playgroud)

这不起作用.如果我用"live"替换"on"就行了.(但是现场折旧)

我知道我能做到这一点

$(document).ready(function() {

    $('div').on('click','a.more', function() {
        var $parent = $(this).parent();
        $parent.text($parent.data('completemessage'));
    });

    $('span.more-available').append($('<a class="more">...more</a>'));
});;?
Run Code Online (Sandbox Code Playgroud)

它有效,但我的问题是......

假设"on"提供了live的所有功能,我错了吗?"on"不会绑定到未来的元素吗?这是正确的行为,还是我做错了什么.

小提琴:http://jsfiddle.net/arishlabroo/pRBke/5/

jquery

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

显示微调器,直到使用http post响应加载iframe

我有两个网站A.com和B.com.我必须在A.com的iframe中嵌入B.com.我不能在B.com做任何改变

B.com仅适用于包含一些帖子数据的帖子请求.我有这个工作如下

<!--This is the div inside which I will embedd the iframe-->
<div id="frameDiv"></div>

<script type="text/javascript">

    //Create iframe
    var $ifr = $('<iframe name="myFrame" id="myFrame"></iframe>');

    //create form
    var $form = $('<form action="B.com" method="post" target="myFrame"></form>');

    //Append hidden field to form to pass postData
    $form.append($('<input type="hidden" name="dataName"><input>').val('data'));

    //Append form to the iframe and then append iframe to the div    
    $('#frameDiv').append($ifr.append($form));

    $form.submit();
</script>
Run Code Online (Sandbox Code Playgroud)

现在,B.com在iframe中完美加载并响应发布请求.但是B.com很慢.我想在#frameDiv中显示一个微调器,直到iframe加载.我怎样才能做到这一点?

这是我试过的:

$('#frameDiv').append($spinnerImage)

//Does not work, fires immediately
$ifr.load(function(){
    //Hide the spinner image
});

//Does not work, fires immediately
$ifr.ready(function(){ …
Run Code Online (Sandbox Code Playgroud)

javascript iframe jquery

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

在控制器上下文可用之前查找请求是否是子操作请求

在一个简单的mvc 4应用程序中,我安装了Ninject.MVC3 nuget包.

这是我的控制器,非常基本,ISomeClass由ninject注入构造函数中.

public class HomeController : Controller
{
    private readonly ISomeClass _someClass;

    public HomeController(ISomeClass someclass)
    {
        _someClass = someclass;
    }

    public ActionResult Index()
    {
        return View();
    }

    [ChildActionOnly]
    public PartialViewResult MiniView()
    {
        return PartialView("miniview", _someClass.GetName());
    }
}
Run Code Online (Sandbox Code Playgroud)

这是SomeClass

public class SomeClass : ISomeClass
{
    private readonly string _someName;

    public SomeClass(string someName)
    {
        _someName = someName;
    }

    public string GetName()
    {
        return _someName;
    }
}
Run Code Online (Sandbox Code Playgroud)

在Index.cshtml视图中我有

@{ Html.RenderAction("MiniView","Home"); }
Run Code Online (Sandbox Code Playgroud)

现在在NinjectWebCommon中,当我去注册服务时,我需要知道请求是否是子动作请求.就像我打电话一样Html.RenderAction.这是我正在尝试但它不起作用.

kernel.Bind<ISomeClass>().To<SomeClass>()
      .WithConstructorArgument("someName", c => IsChildAction(c) ? "Child" …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc ninject asp.net-mvc-4

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