如何为具有out-parameter 的委托定义DynamicMethod ,如下所示?
public delegate void TestDelegate(out Action a);
Run Code Online (Sandbox Code Playgroud)
假设我只想要一个将a参数设置为null调用方法的方法.
请注意,我知道处理这个问题的一个更好的方法是让方法返回Action委托,但这只是一个较大项目的简化部分,并且该方法已经返回一个值,我需要处理out参数除了它,因此问题.
我试过这个:
using System;
using System.Text;
using System.Reflection.Emit;
namespace ConsoleApplication8
{
public class Program
{
public delegate void TestDelegate(out Action a);
static void Main(String[] args)
{
var method = new DynamicMethod("TestMethod", typeof(void),
new Type[] { typeof(Action).MakeByRefType() });
var il = method.GetILGenerator();
// a = null;
il.Emit(OpCodes.Ldnull);
il.Emit(OpCodes.Starg, 0);
// return
il.Emit(OpCodes.Ret);
var del = (TestDelegate)method.CreateDelegate(typeof(TestDelegate));
Action a;
del(out a);
} …Run Code Online (Sandbox Code Playgroud) 是否有任何类似于Flying Saucer项目的开源.NET项目(或端口),它使用iText将HTML呈现为PDF?
我开始了一个新的PyCharm项目,并希望用Mercurial进行版本化.
项目目录中有一个.idea目录,其中包含以下文件(以及我是否对其进行版本控制的假设)
我的假设是否正确?
从这个问题:随机数发生器将数字吸引到范围内的任何给定数字?我之前做过一些研究,因为我之前遇到过这样一个随机数发生器.我记得的只是"穆勒"的名字,所以我想我找到了,在这里:
我可以在其他语言中找到它的大量实现,但我似乎无法在C#中正确实现它.
例如,这个页面,用于生成高斯随机数的Box-Muller方法表示代码看起来应该是这样的(这不是C#):
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
double gaussian(void)
{
static double v, fac;
static int phase = 0;
double S, Z, U1, U2, u;
if (phase)
Z = v * fac;
else
{
do
{
U1 = (double)rand() / RAND_MAX;
U2 = (double)rand() / RAND_MAX;
u = 2. * U1 - 1.;
v = 2. * U2 - 1.;
S = u * u + v * v;
} while …Run Code Online (Sandbox Code Playgroud) 马戏团正在设计一个塔式例程,由站在彼此肩膀上的人组成.出于实际和美学的原因,每个人必须比他或她下面的人更短更轻.考虑到马戏团中每个人的身高和体重,写一种计算这种塔中最大可能人数的方法.
示例:
输入(ht,wt):( 65,100)(70,150)(56,90)(75,190)(60,95)(68,110)
输出:最长的塔长度为6,包括从上到下:(56,90)(60,95)(65,100)(68,110)(70,150)(75,190)
有人向我建议如下:可以按如下方式进行:
最多1和2.
我不明白为什么我们需要同时执行第1步和第2步.不能只做1并找到答案.如果没有,请举例说明只做第1步没有回答?
我们正在观察一些奇怪的东西,代码如下:
var task = new Task(...); // run in the background, do something lengthy work
task.ContinueWith(..., TaskScheduler.FromCurrentSynchronizationContext());
task.Start();
Run Code Online (Sandbox Code Playgroud)
第二个任务调用一个事件,然后尝试更新GUI,我们得到了可怕的跨线程异常.
Thread.CurrentThread.ManagedThreadId从第二个任务中的方法检查表明它实际上没有在UI线程上运行.
这催生了任务的代码是在UI线程上运行.
有什么情况会出错吗?
我有一个脚本,我想继续使用,但看起来我要么找到一些解决方法来解决Python 3中的错误,要么降级回2.6,因此不得不降级其他脚本......
希望有人在这里找到了解决方法.
问题在于,由于Python 3.0中有关字节和字符串的新变化,并非所有的库代码都经过了明显的测试.
我有一个从Web服务器下载页面的脚本.这个脚本在python 2.6中传递了一个用户名和密码作为url的一部分,但在Python 3.0中,这不再起作用了.
例如,这个:
import urllib.request;
url = "http://username:password@server/file";
urllib.request.urlretrieve(url, "temp.dat");
Run Code Online (Sandbox Code Playgroud)
失败,出现此异常:
Traceback (most recent call last):
File "C:\Temp\test.py", line 5, in <module>
urllib.request.urlretrieve(url, "test.html");
File "C:\Python30\lib\urllib\request.py", line 134, in urlretrieve
return _urlopener.retrieve(url, filename, reporthook, data)
File "C:\Python30\lib\urllib\request.py", line 1476, in retrieve
fp = self.open(url, data)
File "C:\Python30\lib\urllib\request.py", line 1444, in open
return getattr(self, name)(url)
File "C:\Python30\lib\urllib\request.py", line 1618, in open_http
return self._open_generic_http(http.client.HTTPConnection, url, data)
File "C:\Python30\lib\urllib\request.py", line 1576, in _open_generic_http
auth = base64.b64encode(user_passwd).strip()
File …Run Code Online (Sandbox Code Playgroud) 如果我有这个:
Type t = typeof(Dictionary<String, String>);
Run Code Online (Sandbox Code Playgroud)
我怎么得到"System.Collections.Generic.Dictionary"一个字符串?这是最好/唯一的方法:
String n = t.FullName.Substring(0, t.FullName.IndexOf("`"));
Run Code Online (Sandbox Code Playgroud)
虽然看起来有点像哈希.
我想要这个的原因是我想要一个Type对象,并产生类似于C#源代码文件中的代码.我正在制作一些文本模板,我需要将类型作为字符串添加到源代码中,并且FullName属性产生如下内容:
System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089]]
Run Code Online (Sandbox Code Playgroud)
而不是我想要的:
System.Collections.Generic.Dictionary<System.String, System.String>
Run Code Online (Sandbox Code Playgroud)
编辑:好的,这是最终的代码,对我来说似乎有点像黑客,但它有效:
/// <summary>
/// This method takes a type and produces a proper full type name for it, expanding generics properly.
/// </summary>
/// <param name="type">
/// The type to produce the full type name for.
/// </param>
/// <returns>
/// The type name for <paramref name="type"/> …Run Code Online (Sandbox Code Playgroud) 我创建了类库,其中一些是世界各地的其他人使用的,现在我开始使用Visual Studio 2010,我想知道切换到使用代码契约是多么好的想法,而不是常规的旧代码 - 风格if语句.
即.而不是这个:
if (fileName == null)
throw new ArgumentNullException("fileName");
Run Code Online (Sandbox Code Playgroud)
用这个:
Contract.Requires(fileName != null);
Run Code Online (Sandbox Code Playgroud)
我问的原因是我知道静态检查器不可用,所以我对我做的一些假设有点紧张,编译器无法验证.当有静态检查程序时,这可能导致类库无法为下载它的人编译.这个,再加上我甚至无法重现这个问题的事实,会让它很难解决,而且我会认为它不会说我的类库的质量如果它看起来甚至没有编译出来的框.
所以我有几个问题:
任何的建议都受欢迎.
编辑:让我澄清一下我的意思.
假设我在类中有以下方法:
public void LogToFile(string fileName, string message)
{
Contracts.Requires(fileName != null);
// log to the file here
}
Run Code Online (Sandbox Code Playgroud)
然后我有这个代码:
public void Log(string message)
{
var targetProvider = IoC.Resolve<IFileLogTargetProvider>();
var fileName = targetProvider.GetTargetFileName();
LogToFile(fileName, message);
}
Run Code Online (Sandbox Code Playgroud)
现在,在这里,IoC启动,解决了一些"随机"类,它为我提供了一个文件名.让我们说对于这个库,没有办法可以找回一个不会给我一个非空文件名的类,但是,由于IoC调用的性质,静态分析无法验证这个,因此可能假设一个可能的值可能为null.
因此,静态分析可能会得出结论,存在LogToFile使用null参数调用方法的风险,因此无法构建.
我理解我可以在代码中添加假设,说编译器应该认为fileName我从该方法返回的那个永远不会为null,但是如果我没有静态分析器(VS2010 Professional),那么代码会为我编译,因此我可能会将此作为一个睡眠错误,让终极找到的人.换句话说,这里可能没有编译时警告可能存在问题,因此我可能会按原样释放库.
这是一个真实的场景和问题吗?
我阅读了本文档:Mercurial分支指南,特别是标题为使用书签分支的部分.
它说:
现在,您在当前变更集中有两个分支(基本上是标记).
要切换到其中一个分支,您可以使用
hg update feature更新到该分支的tip变更集,并将自己标记为在该分支上工作.提交时,它会将书签移动到新创建的变更集.
我尝试了这个,但它最终同时移动了两个书签.
该指南是错误的,过时的,还是我做错了什么?请注意,我知道在单独的分支上设置书签只会移动与我当前正在处理的分支相关的书签,但是该指南(很多人说这是明确的指南)具体说明上述文本,表明它应该通过"告诉"Mercurial我正在研究哪个书签(分支)来工作.
但测试显示不然.
有任何想法吗?
例:
> hg init > echo 1 >test.txt > hg commit -m "initial" --addremove adding test.txt > hg bookmark main > hg bookmark feature > hg log changeset: 0:c56ceb49ee20 tag: feature tag: main tag: tip user: Lasse V. Karlsen <lasse@vkarlsen.no> date: Tue Nov 30 23:06:16 2010 +0100 summary: initial > hg update feature 0 files updated, 0 files merged, 0 files removed, 0 files …
c# ×5
mercurial ×2
.net ×1
algorithm ×1
bookmarks ×1
branch ×1
c#-4.0 ×1
html-to-pdf ×1
itextsharp ×1
pycharm ×1
python ×1
python-3.x ×1
random ×1
reflection ×1
task ×1
types ×1
urllib ×1