我希望能够做到以下几点:
Func<int,bool> tryMethodFunc = TryMethod;
Run Code Online (Sandbox Code Playgroud)
TryMethod的签名如下:
bool TryMethod(int value, int value2 = 0, double value3 = 100.0)
Run Code Online (Sandbox Code Playgroud)
我并不反对将方法分解为一种curry格式,但如果没有这种方法可以做到这一点,那将会更有效.
使用C#3.5我试图在运行时使用反射发射生成动态类型.我使用Microsoft 的Dynamic Query Library示例创建了一个类生成器.一切正常,我的问题是100个生成的类型使内存使用量膨胀大约25MB.这是一个完全不可接受的内存配置文件,因为最终我想支持在内存中生成数十万种类型.
内存分析显示内存显然是由各种System.Reflection.Emit类型和方法持有,虽然我无法弄清楚为什么.我没有找到其他人谈论这个问题所以我希望这个社区中的某个人知道我做错了什么或者这是否是预期的行为.
下面的举例:
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Reflection.Emit;
namespace SmallRelfectExample
{
class Program
{
static void Main(string[] args)
{
int typeCount = 100;
int propCount = 100;
Random rand = new Random();
Type dynType = null;
SlimClassFactory scf = new SlimClassFactory();
for (int i = 0; i < typeCount; i++)
{
List<DynamicProperty> dpl = new List<DynamicProperty>(propCount);
for (int j = 0; j < propCount; j++)
{
dpl.Add(new DynamicProperty("Key" + rand.Next().ToString(), …
Run Code Online (Sandbox Code Playgroud) 我不明白为什么SortedDictionary的性能比Dictionary设置和检索值慢约5倍.我希望插入和删除更慢但不更新或检索.我测试了.Net 3.5和.Net 4.0发布的编译代码.预先计算了一组随机密钥,以确保随机变化不对随机访问的差异负责.
以下是测试的以下场景.
谁知道为什么性能差异?
如果我做错了什么或愚蠢,请指出.
示例代码:只需使用SortedDictionary切换Dictionary以测试差异.
const int numLoops = 100;
const int numProperties = 30;
const int numInstances = 1000;
static void DictionaryBench(int numLoops, int numValues, int numInstances, string[] keyArray)
{
Stopwatch sw = new Stopwatch();
double total = 0.0d;
for (int j = 0; j < numLoops; j++)
{
//sw.Start();
Dictionary<string, object> original = new Dictionary<string, object>(numValues);
for (int i = 0; i < numValues; i++)
{
original.Add(String.Format("Key" + i.ToString()), "Value0:" + …
Run Code Online (Sandbox Code Playgroud)