小编Mat*_*son的帖子

是否可以重命名为WiX C#自定义操作生成的类和程序集(对于Visual Studio)?

当我使用WiX 3.x和Visual Studio 2010创建名为"MyCustomActions"的Windows Installer XML"C#Custom Action Project"时,它会生成以下文件:

"CustomAction.config","CustomAction.cs"和项目文件"MyCustomActions.csproj".

我们使用包含公司名称的程序集命名方案,因此我们将程序集名称更改为:CompanyName.ProductName.MyCustomActions

同时,我们将CustomActions类的默认命名空间和实际命名空间更改为CompanyName.ProductName.MyCustomActions,以匹配程序集名称.

可以在不更改任何其他文件或类名的情况下执行此操作吗?

我想知道的是使用"CustomAction.config"文件 - 它的名称是否必须以某种方式与输出程序集名称相关?我不确定如何使用"CustomAction.config".

另外:"CustomAction.cs"文件包含一个名为"CustomActions"的类(注意复数),我们的命名约定说类的代码文件应该与类本身的名称相同,所以我们想重命名代码文件为"CustomActions.cs".我很确定没问题......是吗?

最后,如果我们将CustomAction类重命名为其他类,那会没关系吗?我再次关注它与"CustomAction.config"的关系.

我想所有这些问题都可以归为一类:"CustomAction.config"文件的名称与命名空间,程序集和类名的其余部分之间的关​​系是什么?

如果我有答案,它将回答我之前的所有问题.

c# custom-action wix

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

Visual Studio 2012:"实时错误列表" - 如何将其关闭?

当我在Visual Studio 2012中编辑C#代码时,"错误列表"窗口会不断更新当前语法错误列表等.我发现这非常分散注意力,并希望将其关闭.

有谁知道怎么关掉它?

(我真的不知道这个VS功能被正确调用了什么,所以有人可能已经回答了它......如果是这样,请道歉.)

c# ide visual-studio-2012

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

使Guid属性线程安全

我的一个类有一个Guid类型的属性.此属性可以由多个线程同时读取和写入.我的印象是对Guid的读写不是原子的,因此我应该锁定它们.

我选择这样做:

public Guid TestKey
{
    get
    {
        lock (_testKeyLock)
        {
            return _testKey;
        }
    }

    set
    {
        lock (_testKeyLock)
        {
            _testKey = value;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

(在我的课程中,对Guid的所有访问也是通过该属性完成的,而不是直接访问_testKey.)

我有两个问题:

(1)是否真的有必要像这样锁定Guid以防止撕裂读数?(我很确定它是.)

(2)这是一种合理的锁定方式吗?或者我需要像下面这样做:

get
{
    Guid result;

    lock (_testKeyLock)
    {
        result = _testKey;
    }

    return result;
}
Run Code Online (Sandbox Code Playgroud)

[编辑]本文确认Guids将遭受撕裂的读取:http://msdn.microsoft.com/en-us/magazine/jj863136.aspx

c# multithreading

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

Resharper警告将枚举枚举到UIntPtr,但没有编译器警告

在下面的代码中,Resharper给了我一个警告:Cannot cast expression of type 'Color' to type 'UIntPtr'.(实际上,Resharper认为这是一个实际错误.)

但是,没有编译器警告,它工作正常.

这对我来说看起来像一个Resharper bug.是吗?或者编译器不担心它有什么不好吗?(我正在使用Resharper 7.1.1)

using System;

namespace Demo
{
    internal class Program
    {
        public enum Color { Red, Green, Blue }

        private static void Main(string[] args)
        {
            UIntPtr test = (UIntPtr) Color.Red; // Resharper warning, no compile warning.
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我可以通过首先将值转换为int来使警告消失,所以我有一个解决方法:

UIntPtr test = (UIntPtr)(int) Color.Red;
Run Code Online (Sandbox Code Playgroud)

c# resharper resharper-7.1

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

StartsWith的扩展,用于搜索所有单词

是否有字符串的StartsWith的扩展,它在字符串中的每个单词的开头搜索?

像: "Ben Stiller".StartsWithExtension("Sti")回归真实

我想要这个,所以我可以做一个搜索的谓词.

假设有一个名为Persons的列表,ICollection<Person>
每个人都有一个属性Name,其值为"Ben Stiller"或"Adam Sandler".

我希望能够做如下的谓词:

Persons.Where(p => p.Name.StartsWithExtension(query))

谢谢(欢迎其他更好的方法来实现这一目标)

c# linq search startswith autosuggest

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

FileStream.SafeFileHandle*是否真的*将当前流的位置设置为0?

根据MSDN文档FileStream.SafeFileHandle:

SafeFileHandle属性自动刷新流并将当前流位置设置为0.这允许使用此属性返回的SafeFileHandle移动文件或使用另一个流重置流位置.

但是,我的测试似乎表明流位置没有改变.

请考虑以下代码:

using System;
using System.IO;

namespace Demo
{
    internal static class Program
    {
        public static void Main()
        {
            Directory.CreateDirectory("C:\\TEST");
            var buffer = new byte[1024];

            using (var file = new FileStream("C:\\TEST\\TEST.BIN", FileMode.Create))
            {
                file.Write(buffer, 0, buffer.Length);
                Console.WriteLine(file.Position);  // Prints 1024
                var dummy = file.SafeFileHandle;
                // dummy.Dispose();                // Uncommenting this line will make the next line throw.
                Console.WriteLine(file.Position);  // Still prints 1024!
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果SafeFileHandle确实访问确实将当前流位置重置为0,我预计第二个WriteLine()将打印0.

我有其他测试,我实际使用SafeFileHandleWindows API ReadFile()和WriteFile()方法,即使这样,它似乎不会更改文件指针. …

c# filestream safefilehandle

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

如果未指定年份,如何使 DateTime.TryParse() 失败?

考虑以下代码来解析日期。(请注意,我处于 ​​EN-gb 语言环境):

const DateTimeStyles DATE_TIME_STYLES = DateTimeStyles.NoCurrentDateDefault | DateTimeStyles.AllowWhiteSpaces;
DateTime dt;

// Current culture for this example is "EN-gb".

if (DateTime.TryParse("31/12", CultureInfo.CurrentUICulture, DATE_TIME_STYLES, out dt))
    Console.WriteLine("Parsed correctly"); // Do not want!
else
    Console.WriteLine("Did not parse correctly.");
Run Code Online (Sandbox Code Playgroud)

我故意省略了年份。但是,TryParse()将解析此数据而不会出现任何错误,并将替换当前年份。

我希望能够强制用户输入日期的所有组成部分(使用其本地格式),因此我希望上述解析失败 - 或者能够检测到用户没有输入一年。

我真的不想使用DateTime.TryParseExact(),因为那样我就必须添加代码来为所有不同的受支持区域设置指定所有不同的有效格式,这非常重要并且可能容易出错。不过,我怀疑这可能是我唯一明智的选择。

有人有什么想法吗?(这里有人已经实施了一个“解决方案”,其中涉及不允许当前年份,这显然不是一个好的解决方案......)

c# datetime parsing

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

避免对具有可为空类型/字段的泛型发出警告的最佳方法?

考虑以下人为的类:

public sealed class Test<T>
{
    public void SetItem(T item)
    {
        _item = item;
    }

    public T GetItem()
    {
        return _item;
    }

    T _item; // warning CS8618: Non-nullable field '_item' is uninitialized.
}            // Consider declaring the field as nullable.
Run Code Online (Sandbox Code Playgroud)

假设它必须使用以下代码:

var test1 = new Test<int>();
test1.SetItem(1);
Console.WriteLine(test1.GetItem());

var test2 = new Test<string>();
test2.SetItem("One");
Console.WriteLine(test2.GetItem());
Run Code Online (Sandbox Code Playgroud)

进一步假设nullable在编译时启用。

这个实现会产生一个编译器警告(如果像我们一样,你启用了“警告作为错误”,它就会变成一个错误):

警告 CS8618:不可为空的字段“_item”未初始化。考虑将该字段声明为可为空的。

我的问题很简单:

避免此警告的最佳方法是什么?

我尝试过的事情:

1) 将 _item 声明为可空:

T? _item; // Error: A nullable type parameter must be known to …
Run Code Online (Sandbox Code Playgroud)

c# c#-8.0 nullable-reference-types

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

调试控制台应用程序时,Visual Studio会陷入异常报告循环.为什么?

考虑这个简单的控制台应用

using System;

namespace Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            throw new Exception();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我在Visual Studio 2010或Visual Studio 2012 Beta中的调试器下运行它.

当我这样做时,调试器自然会在异常处停止.好到目前为止.

但是当我按F5继续(或选择Debug | Continue)时,它再次停止在同一个异常.我必须停止调试程序才能退出.当我按下F5时,我希望程序退出.

有谁知道它为什么会这样做?

[编辑]

我已将回复标记为答案,但要查看调试器行为的奇怪后果,请考虑以下代码:

using System;

namespace Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            throw new Exception();
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Console.WriteLine("Unhandled Exception.");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在调试器下运行此命令并按F5一次,然后查看输出.你会看到很多"Unhandled Exception"消息,尽管代码实际上只抛出一次.调试器导致异常被抛出多次!这就是我觉得奇怪的事情.

c# visual-studio-2010

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

编写C#属性的最短方式?

有人告诉我你可以替换以下代码:

private string name;

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

以下内容并没有任何不良影响:

public string Name;
Run Code Online (Sandbox Code Playgroud)

我意识到,像第一个例子中那样设置的属性与我删除它并设置原始属性时的属性几乎相同,public但是对于你需要的属性的第二种方式是不好的编程习惯基本的吸气剂和二传手?

c# syntax standards

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