小编Che*_*hen的帖子

使用Linq选择类的属性以返回IEnumerable <T>

如果我有一个SortedList<int, MyClass>并且我想IEnumerable<T>从该类返回一个新属性,我该怎么做?

我试过SortedList.Select(x=>x.MyProperty, x.AnotherProperty)但它不起作用.

谢谢.

.net c# linq .net-3.5 c#-3.0

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

AsParallel() - 在asp.net中并行使用2个以上的线程

我有一个方法,我用不同的参数调用8次.我用

 AvailableYears.AsParallel()
             .Select<Int32,DateUsedByThread>(x => GetDataForYearWorker(x,CIF))
             .ToList();
Run Code Online (Sandbox Code Playgroud)

GetDataForYearWorker同步从Web服务获取响应.它在我的asp.net应用程序上使用非常少的计算能力,但是每个webservice响应都需要3-5秒.因为对web服务的调用是彼此独立的,所以我想同时进行所有操作.但看起来只有2个线程可以同时运行.为什么这样,我怎么能有8个线程同时工作?

c# asp.net parallel-processing multithreading

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

覆盖配置设置

我有一个在几个项目中使用的配置文件general.config,看起来像:

<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
   <add key="mykey1" value="myvalue1"/>    
   <add key="mykey2" value="myvalue2"/>
</appSettings>
Run Code Online (Sandbox Code Playgroud)

在其中一个项目中,我需要覆盖两个设置中的一个.所以app.config这个项目看起来像:

<?xml version="1.0"?>
<configuration>
  <appSettings file="general.config">
    <remove key="mykey1"/>
    <add key="mykey1" value="anothervalue"/>
    <add key="mykey3" value="myvalue3"/>
  </appSettings>  
</configuration>
Run Code Online (Sandbox Code Playgroud)

但是remove不在这里工作.如何在mykey1不破坏的情况下覆盖mykey2add适用于这种情况.我可以myvalue3ConfigurationManager.

编辑:general.config编译时自动复制到输出文件夹.不要担心路径问题.目前我得到了:

ConfigurationManager.AppSettings["mykey1"] 
     //I got "myvalue1", but I want "anothervalue" here
     //that is, this item is "overrided", just like virtual methods in C#
ConfigurationManager.AppSettings["mykey2"] 
     //this setting will not be modified, currently it works …
Run Code Online (Sandbox Code Playgroud)

.net c# configuration app-config

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

根据模板方法中的不同类型的不同代码路径

假设我有一个这样的模板类:

template<typename TRequest, typename TResponse = void>
class handler
{
private:
    TResponse process_core(const TRequest& request);
public:
    TResponse process(const TRequest& request)
    {
        //log the request        
        TResponse response = process_core(request);  //return process_core(request) works;
        //log the response, for void it's fine to log nothing
        return response;
    }
};
Run Code Online (Sandbox Code Playgroud)

项目中的其他地方process_core是针对不同的TRequest/TResponse类型实现的.例如:

template<> void handler<Foo>::process_core(const Foo& foo) { }
template<> Baz handler<Bar, Baz>::process_core(const Bar& bar) { }
Run Code Online (Sandbox Code Playgroud)

显然return response打破了void类型.这样做的正确方法是什么?或者我的设计不是C++方式?我是C++的新手.

c++ templates template-classes

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

FormsAuthentication.Decrypt始终在其中一个Web服务器中返回null

我有一个基于asp.net 4.0的webform应用程序,部署到两个不同的服务器.webform应用程序只有一个Default.aspx及其代码:

protected void Page_Load(object sender, EventArgs e)
{
    MachineKeySection section =
     (MachineKeySection)ConfigurationManager.GetSection("system.web/machineKey");

    this.Response.Write(section.DecryptionKey);
    this.Response.Write("<br />");

    this.Response.Write(section.ValidationKey);
    this.Response.Write("<br />");

    var authToken = "xxxxxx";  
        //the real token is obviously not xxx, just an example here

    this.Response.Write(authToken);
    this.Response.Write("<br />");

    var ticket = FormsAuthentication.Decrypt(authToken);
    if (ticket != null) this.Response.Write(ticket.Name);
    this.Response.End();
}
Run Code Online (Sandbox Code Playgroud)

具有相同web.config的相同代码部署到两个Web服务器.但是,其中一个工作正常,另一个总是ticket等于null.如果我删除,if (ticket != null)则抛出一个空引用异常.它们具有完全相同的输出,除了票证部分.

Web服务器在Windows Server 2008 R2 SP1上运行,并安装了.NET Framework 4.我确定两个Web服务器上的代码完全相同,包括machineKey:

<machineKey validationKey="xxx" decryptionKey="yyy" validation="SHA1" decryption="AES" />
Run Code Online (Sandbox Code Playgroud)

怎么会发生这种情况?你对这个问题有什么看法吗?

UPDATE

MS BUG,需要更新软件包:http://support.microsoft.com/kb/2656351

c# asp.net asp.net-mvc webforms

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

反向表达式<Func <T,TResult >>.编译()?

既然我们可以:

Expression<Func<int, bool>> predicate = x => x > 5;
var result = Enumerable.Range(0,10).Where(predicate.Compile());
Run Code Online (Sandbox Code Playgroud)

我怎么能够:

Func<int,bool> predicate = x => x > 5;
Expression<Func<int,bool>> exp = predicate.Decompile();
Run Code Online (Sandbox Code Playgroud)

也就是说,我想得到相应ExpressionFunc.可能吗?

.net c# linq lambda expression-trees

8
推荐指数
2
解决办法
2894
查看次数

是否有一个委托,它不是C#中的MulticastDelegate?

我觉得答案是否定的?如果没有,我们为什么要分开DelegateMulticastDelegate上课?也许是因为"其他一些.NET语言"?

.net c# delegates multicastdelegate

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

EqualityComparer <T>.默认不够聪明

我正在阅读源代码,EqualityComparer<T>.Default发现它并不那么聪明.这是一个例子:

enum MyEnum : int { A, B }
EqualityComparer<MyEnum>.Default.Equals(MyEnum.A, MyEnum.B)
//is as fast as 
EqualityComparer<int>.Default.Equals(0, 1)

enum AnotherEnum : long { A = 1L, B = 2L }
//is 8x slower than
EqualityComparer<long>.Default.Equals(1L, 2L)
Run Code Online (Sandbox Code Playgroud)

原因很明显来自EqualityComparer中私有方法的源代码.

private static EqualityComparer<T> CreateComparer()
{
    //non-important codes are ignored
    if (c.IsEnum && (Enum.GetUnderlyingType(c) == typeof(int)))
    {
        return (EqualityComparer<T>) RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType) typeof(EnumEqualityComparer<int>), c);
    }
    return new ObjectEqualityComparer<T>();
}
Run Code Online (Sandbox Code Playgroud)

我们可以看到EqualityComparer<int>.Default,EqualityComparer<MyEnum>.DefaultEqualityComparer<long>.Default得到一个明智的比较器,其Equals方法如下:

public static bool Equals(int x, int y) …
Run Code Online (Sandbox Code Playgroud)

.net c# enums equals iequalitycomparer

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

为什么GetCustomAttributes返回object []而不是Attribute []?

只是好奇,看MemberInfo.GetCustomAttributes.是否暗示它可能包含非属性对象?

.net c# reflection attributes

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

const与readonly

今天我发现了一篇文章,其中一个const字段称为编译时常量,而一个readonly字段称为运行时常量.这两个短语来自"Effective C#".我在MSDN和语言规范中搜索,找不到运行时常量.

没有攻击性,但我不认为运行时常量是一个恰当的短语.

private readonly string foo = "bar";
Run Code Online (Sandbox Code Playgroud)

创建一个名为"foo"的变量,其值为"bar",值为readonly,这里它是一个变量,没有业务constant.只读变量仍然是变量,它不能是常量.变量和常量是互斥的.

也许这个问题太过分了,我还是想听听别人的意见.你怎么看?

.net c# const readonly

8
推荐指数
2
解决办法
7805
查看次数