小编Hop*_*ess的帖子

在什么情况下Process.Start()方法返回false?

来自MSDN:

返回值true表示已启动新的进程资源.如果StartInfo属性的FileName成员指定的进程资源已在计算机上运行,则不会启动其他进程资源.而是重用正在运行的进程资源并返回false.

尝试这样的事情:

var info = new ProcessStartInfo {FileName = @"CMD"};

var p1 = new Process
{
     StartInfo = info
};

var result = p1.Start(); //true
result = p1.Start(); //true

var p2 = new Process
{
    StartInfo = info
};

result = p2.Start(); //true
Run Code Online (Sandbox Code Playgroud)

如果我使用FilePath = @"c:\myapp.exe"而不是使用相同的结果CMD.

在什么情况下会返回false

.net c#

12
推荐指数
2
解决办法
1684
查看次数

何时使用属性注入?

  1. 什么时候应该使用属性注入?
  2. 如果完全控制实例创建,我应该默认使用构造函数注入吗?
  3. 我是否正确使用构造函数注入我编写与容器无关的代码?

dependency-injection ioc-container

11
推荐指数
2
解决办法
1055
查看次数

如何使用 EF 迁移向现有表添加新列并根据现有列设置其值

例如,有一个包含值的表:

Id CarModel
1  Passat
2  Land Cruiser
Run Code Online (Sandbox Code Playgroud)

并且需要添加不可为空的列Manufacturer。制造商的初始值(对于现有记录)应为:

For CarModel=Passat - VW 
For CarModel=Land Cruiser - Toyota
etc.
Run Code Online (Sandbox Code Playgroud)

我的迁移:

protected override void Up(MigrationBuilder migrationBuilder)
{
  migrationBuilder.AddColumn<string>(
  name: "Manufacturer",
  table: "Cars",
  nullable: false);
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

.net entity-framework-core

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

非静态类中的专用静态字段

有一个这样的课:

class Application
{
    private Process _process; 

    private static string _applicationPath = string.Empty;

    public void Start(string arguments)
    {
        if (!File.Exists(_applicationPath))
            Deploy();
        _process = Process.Start(_applicationPath, arguments);
    }

    public void SomeMethod()
    {
        //method that manipulate _process
    }

    private void Deploy()
    {
        // copying, installation steps (takes some time) and assign _applicationPath
    }
}
Run Code Online (Sandbox Code Playgroud)
  1. 我使用静态字段是不是很糟糕?预计Application的其他实例将使用相同的_applicationPath.
  2. 这是违反SRP原则(SOLID)的一个例子吗?我应该向其他班级提取"部署责任"吗?

c# solid-principles

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

"未找到方法"例外.为什么AppDomain.CurrentDomain.AssemblyResolve不起作用?

  1. 有一个应用程序(executor.exe)使用反射调用类库(lib.dll)中的方法.
  2. executor.exe将程序集Newtonsoft.Json 8.0作为嵌入式资源.
  3. lib.dll引用了Newtonsoft.Json 9.0版.
  4. lib.dll引用了system.net.http.formatting版本4.0.0.21112,后者又引用了Newtonsoft.Json 4.5.
  5. 我没有机会修改executor.exe.config(测试除外).

我想得到什么:

new JsonMediaTypeFormatter().SerializerSettings;
Run Code Online (Sandbox Code Playgroud)

从lib.dll调用.但它失败了:

找不到方法:'Newtonsoft.Json.JsonSerializerSettings System.Net.Http.Formatting.JsonMediaTypeFormatter.get_SerializerSettings()'

我想做什么:

  1. 处理AppDomain.CurrentDomain.AssemblyResolve(使用ModuleInitializer正确订阅).但它并没有上升.崩溃后有2个Newtonsoft.Json(带有不同的版本)加载到AppDomain.
  2. 在app config中绑定:

     <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed"
     culture="neutral" />
     <bindingRedirect oldVersion="4.0.0.0-5.0.0.0" newVersion="9.0.0.0" />
    
    Run Code Online (Sandbox Code Playgroud)

是的,它有效.但我不能使用这个解决方案.传递后将2个Newtonsoft.Json(带有不同版本)加载到AppDomain.

  1. 我不明白为什么会这样(oldVersion ="8.0.0.0-9.0.0.0")但是:

    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
    <bindingRedirect oldVersion="8.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
    
    Run Code Online (Sandbox Code Playgroud)

异常"未找到方法"不抛出.传递后将1个Newtonsoft.Json(9.0)加载到AppDomain.但不适合我.

为什么AppDomain.CurrentDomain.AssemblyResolve不起作用?我想问题是在2个加载的程序集中,但我不能改变这种行为.

.net c#

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

在foreach或GetEnumerator()中产生?

在这种情况下哪个更好:

IEnumerator<Cat> EnumerateCats()
{
    var rawCats = GetRawCats();

    foreach(var cat in rawCats)
    {
        var typedCat = new Cat
        {  
            Name = cat.Key;
            Breed = cat.Value;
        };

        yield return typedCat;
    }
}
Run Code Online (Sandbox Code Playgroud)

要么

IEnumerator<Cat> EnumerateCats()
{
    return GetRawCats()
       .Select(cat => new Cat
        {  
            Name = cat.Key;
            Breed = cat.Value;
        })
       .GetEnumerator();
}
Run Code Online (Sandbox Code Playgroud)

我更喜欢上一个代码示例.它的工作方式与第一个相同吗?

c#

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

使用处理后通过一次性物体的方法返回的对象

有这样的代码:

using(var disposableObject = new MyClass()) 
{
    var something = disposableObject.GetSomething();
    return something; 
}
Run Code Online (Sandbox Code Playgroud)

所以,我返回的不是disposableObject,而是由disposableObject生成的对象.这样对吗?

c#

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

如何使用 WIQL 查询具有指定注释的工作项链接

我需要一种快速的方法来获取按链接注释过滤的链接列表。有一个代码:

var linkCommentFilter = "Some link comment";
const string queryString = @"SELECT [System.Id]
                               FROM workItemlinks
                               WHERE [System.Links.LinkType] = 'Tested By'
                               AND [System.Links.Comment] = '{0}'
                               AND ([Source].[System.Id] IN ({1}))";

var query = new Query(store, string.Format(queryString, 
    linkCommentFilter,
    string.Join(",", wiIds)));
var result = query.RunLinkQuery().ToArray();
Run Code Online (Sandbox Code Playgroud)

尝试运行此代码时发生异常“字段 System.Links.Comment 不存在”。我该如何解决它?

tfs wiql

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