小编Dot*_*ent的帖子

使用委托和Lambda的奇怪行为

作为在我正在开发的库中引入延迟格式化评估的一种方法,我已经定义了委托

public delegate string MessageFormatterDelegate(string message, params object[] arguments);
public delegate string MessageFormatterCallback(MessageFormatterDelegate formatterDelegate);
Run Code Online (Sandbox Code Playgroud)

以及下一课的内容

public static class TestClass
{
    public static string Evaluate(MessageFormatterCallback formatterCallback)
    {
        return (formatterCallback(String.Format));
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,这表现得非常奇怪:从外部项目运行时,语句

Console.WriteLine(TestClass.Evaluate(message => message("{0},{1},{2}", 1, 2, 3)));
Run Code Online (Sandbox Code Playgroud)

没有编译,与错误而失败

Error   1   Delegate 'MessageFormatterDelegate' does not take 4 arguments
Run Code Online (Sandbox Code Playgroud)

Console.WriteLine(TestClass.Evaluate((MessageFormatterDelegate message) => message("{0},{1},{2}", 1, 2, 3)));
Run Code Online (Sandbox Code Playgroud)

编译和工作没有问题,1,2,3在控制台中打印.为什么我必须message使用MessageFormatterDelegate第二个lambda表达式中的类型限定参数?有没有办法来控制这种行为?

c# lambda .net-3.5

15
推荐指数
2
解决办法
1089
查看次数

Ninject - 静态类中的内核?

是否正确在一个单独的共享库StandardKernel中用NinjectModule静态类"包装"一个s,并在需要注入时使用相同的库(而不是每次都实例化一个新内核)?

编辑:我正在尝试从我正在开发的WCF服务中使用Ninject.

(如果我刚开始学习DI和IoC容器,我说的话完全不好意思,请耐心等待)

c# wcf dependency-injection ninject inversion-of-control

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

需要帮助SelectMany

假设,我有一个IEnumerable<Tuple<string, object>>和我想要枚举的每个元素的第一个元素由(几个)其他元素的列表:

Original enumerable: { { "a", obj1 }, { "b", obj2 } }
First-element replacement: a -> { "c", "d" }, b -> { "e", "f", "g" }
Result : { { "c", obj1 }, { "d", obj1 }, { "e", obj2 }, { "f" , obj2 }, { "g" , obj2 } }
Run Code Online (Sandbox Code Playgroud)

我怎样才能SelectMany以更好的方式实现这一目标

enumerable.SelectMany(item => ReplacementFunction(item.Item1).Select(newItem =>
new Tuple<string, object>(newItem, item.Item2)))
Run Code Online (Sandbox Code Playgroud)

c# linq

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

通用类型例外

最近我遇到了在泛型方法中使用给定消息创建异常的问题.例如,以下代码按预期工作:

public static void Throw<T>() where T : Exception, new()
{
    throw new T();
}

...

public static void Main()
{
    Throw<ArgumentOutOfRangeException>(); // Throws desired exception but with a generic message.
}
Run Code Online (Sandbox Code Playgroud)

但是,我希望能够写

public static void Throw<T>(string message) where T : Exception, new()
{
    T newException = new T();

    newException.Message = message; // Not allowed. 'Message' is read-only.

    throw newException;
}

...

public static void Main()
{
    Throw<ArgumentOutOfRangeException>("You must specify a non-negative integer."); // Throws desired exception.
}
Run Code Online (Sandbox Code Playgroud)

有没有办法在不使用反射来改变Message …

c# generics .net-3.5

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

AJAX URL和URL重写

我开始建立一个个人网站,我希望它的布局看起来像

-------------------------------
- Page Header & Menus Go Here -
-------------------------------
-        Main Contents        -
-------------------------------
-           Footers           -
-------------------------------
Run Code Online (Sandbox Code Playgroud)

主要的问题是我希望它是一个单页面界面,其中主要内容加载和显示与AJAX和jQuery的组合,以产生一个很好的效果.但是,我当然希望将内容添加到书签并由搜索引擎编入索引.我已经浏览了单页界面宣言,它解释了实现这一目标的一些好方法,但我真的不想让我的网址像

http://www.mysite.com/index.php#!section=section1

http://www.mysite.com/index.php#!section=section2

当然,我想把它们重新编写为

http://www.mysite.com/section1

http://www.mysite.com/section2

我的问题是这种方法是否正确/可行,以及AJAX URL是否与URL重写兼容.不管怎样,Google会将哪些网址编入索引?

ajax url-rewriting

0
推荐指数
1
解决办法
4131
查看次数

PHP和URL重写

我需要一些帮助来弄清楚以下URL重写规则的含义.我可以理解前三行,但我遇到了这个index.php/$1部分.这/条规则的确切含义是什么?我总是期望在文件名之后看到的唯一事情是查询字符串分隔符(?).这是我第一次看到它/作为分隔符.它究竟意味着什么?

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteRule ^(.*)$ index.php/$1 [PT,L]     
</IfModule>
Run Code Online (Sandbox Code Playgroud)

php url-rewriting

0
推荐指数
1
解决办法
1817
查看次数