关于使反思飞行和探索代表的问题 ......
如果我需要为Func<T, TResult>动态加载类型的方法创建委托,我可能会使用(1)Delegate.CreateDelegate(2)DynamicMethod(3)Expression树.
让我们说一组动态加载的类型/方法在应用程序启动时通过config反映一次,并在应用程序的整个生命周期中使用(启动性能不是问题,也不是内存),代理被缓存并分派到一个强烈的方式.这些代理是同时访问的热路径.
您更喜欢哪种动态绑定方法?为什么?
以下代码是消息批处理例程的概念证明.我是否goto像瘟疫一样避免并重写此代码?或者您认为这goto是一种表达方式吗?
如果你重写,请发一些代码......
var queue = new Queue<TraceItem>(this.batch);
while (this.connected)
{
byte[] buffer = null;
try
{
socket.Recv(out buffer);
}
catch
{
// ignore the exception we get when the socket is shut down from another thread
// the connected flag will be set to false and we'll break the loop
}
HaveAnotherMessage:
if (buffer != null)
{
try
{
var item = TraceItemSerializer.FromBytes(buffer);
if (item != null)
{
queue.Enqueue(item);
buffer = null;
if (queue.Count < this.batch …Run Code Online (Sandbox Code Playgroud) 首先,我希望基于上下文的存储在整个框架中保持一致!
话虽如此,我正在寻找一种优雅的解决方案,使这些属性在ASP.NET,WCF和任何其他多线程.NET代码中都是安全的.这些属性位于一些低级跟踪帮助程序中(如果您想知道它们为什么是内部的,则通过方法公开).
我宁愿不依赖于不需要的程序集(如System.Web等).我不想要求使用此代码的任何人配置任何东西.我只是想让它起作用;)虽然这可能太高了一个订单......
任何人都有任何诡计?(我看过Spring的实现)
internal static string CurrentInstance
{
get
{
return CallContext.LogicalGetData(currentInstanceSlotName) as string;
}
set
{
CallContext.LogicalSetData(currentInstanceSlotName, value);
}
}
internal static Stack<ActivityState> AmbientActivityId
{
get
{
Stack<ActivityState> stack = CallContext.LogicalGetData(ambientActivityStateSlotName) as Stack<ActivityState>;
if (stack == null)
{
stack = new Stack<ActivityState>();
CallContext.LogicalSetData(ambientActivityStateSlotName, stack);
}
return stack;
}
}
Run Code Online (Sandbox Code Playgroud)
更新
安全我不是说同步.背景在这个问题上这里
这个小程序找到文件中十大最常用的单词.您将如何通过逐行流式处理文件来优化此方法,还是将它保持为现在的功能样式?
static void Main(string[] args)
{
string path = @"C:\tools\copying.txt";
File.ReadAllText(path)
.Split(' ')
.Where(s => !string.IsNullOrEmpty(s))
.GroupBy(s => s)
.OrderByDescending(g => g.Count())
.Take(10)
.ToList()
.ForEach(g => Console.WriteLine("{0}\t{1}", g.Key, g.Count()));
Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)
这是我想使用的行读者:
static IEnumerable<string> ReadLinesFromFile(this string filename)
{
using (StreamReader reader = new StreamReader(filename))
{
while (true)
{
string s = reader.ReadLine();
if (s == null)
break;
yield return s;
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:
我意识到顶级单词的实现没有考虑到标点符号和所有其他细微差别,我并不太担心.
澄清:
我对解决方案感兴趣,它不会立即将整个文件加载到内存中.我想你需要一个数据结构,可以在运行中获取一系列单词和"组" - 就像一个trie.然后以某种方式以懒惰的方式完成它,以便线路阅读器可以逐行进行业务.我现在意识到这要求很多,并且比我上面给出的简单例子复杂得多.也许我会试一试,看看我是否可以像上面那样清楚地获得代码(带有一堆新的lib支持).
c# ×4
.net ×2
delegates ×1
dynamic ×1
file-io ×1
goto ×1
httpcontext ×1
optimization ×1
reflection ×1