为什么匿名方法存在闭包?为什么不直接将状态传递给方法而不会在复制闭包变量的情况下生成新类的开销?这不仅仅是"让一切都变得全球化"的倒退吗?
有人跟我说话,我觉得我在这里错过了一些东西......
我需要在终端服务器上实现应用程序的单个VB.NET实例.为此,我使用了Flawless Code博客中的代码.它运行良好,除了代码是用C#编写并使用VB.NET不支持的匿名方法.我需要重写以下内容,以便将其用作VB.NET中的事件.
static Form1 form;
static void singleInstance_ArgumentsReceived(object sender, ArgumentsReceivedEventArgs e)
{
if (form == null)
return;
Action<String[]> updateForm = arguments =>
{
form.WindowState = FormWindowState.Normal;
form.OpenFiles(arguments);
};
form.Invoke(updateForm, (Object)e.Args); //Execute our delegate on the forms thread!
}
}
Run Code Online (Sandbox Code Playgroud) 我正在UserControl动态创建一些s LoadControl(String)并希望订阅每个事件.
我的所有控件都继承了一个Interface需要执行common的公共Event:
public interface IObjectProcessor
{
event EventHandler<ObjectProcessedEventArgs> ObjectProcessed;
}
Run Code Online (Sandbox Code Playgroud)
所以我在我的页面加载事件中做下一步:
protected void Page_Load()
{
switch(Request["type"])
{
case "user":
{
LoadControl("AddUser.ascx", delegate(object sender, ObjectProcessedEventArgs e)
{
// do something
});
break;
}
}
}
private void LoadControl(string path, Action<object, ObjectProcessedEventArgs> action)
{
var control = (IObjectProcessor)LoadControl(path)
control.ObjectProcessed // here!
}
Run Code Online (Sandbox Code Playgroud)
如何订阅此事件的删除?
我被要求解释匿名方法的丑陋和优点.
我可能会解释
丑陋的事情
anonymous methods turning quickly into spaghetti code.
Run Code Online (Sandbox Code Playgroud)
好处
我们可以使用匿名方法生成线程安全代码:示例
static List<string> Names = new List<string>(
new string[] {
"Jon Skeet",
"Marc Gravell",
"David",
"Bill Gates"
});
static List<string> FindNamesStartingWith(string startingText)
{
return Names.FindAll(
delegate(string name)
{
return name.StartsWith(startingText);
});
}
Run Code Online (Sandbox Code Playgroud)
但实际上我不知道它是否是线程安全的.我被要求证明它是正确的.任何人都可以帮助我理解 (1)匿名方法的优点(2)上面的代码线程是否安全?
我有一个方法需要有条件地执行一个方法,如下所示:
int MyMethod(Func<int> someFunction)
{
if (_someConditionIsTrue)
{
return someFunction;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我希望能够将Linq查询作为someFunction传递给MyMethod:
int i = MyMethod(_respository.Where(u => u.Id == 1).Select(u => u.OtherId));
Run Code Online (Sandbox Code Playgroud)
我该怎么做呢?
假设以下代理"调用者"签名:
FuncCaller<T>(Func<T, bool> predicate)
Run Code Online (Sandbox Code Playgroud)
和匹配方法:
bool MyFunc(object o)
Run Code Online (Sandbox Code Playgroud)
什么时候T是引用类型,我可以像这样MyFunc 隐式调用:
FuncCaller<String>(MyFunc) // valid
Run Code Online (Sandbox Code Playgroud)
相反,如果T是值类型,则在隐式调用MyFunc时会出现编译错误:
FuncCaller<Int32>(MyFunc) // invalid ("No overload for 'MyFunc(object)' matches delegate 'System.Func<int?,bool>'")
Run Code Online (Sandbox Code Playgroud)
我的问题是,鉴于这两个例子,为什么MyFunc在隐式调用时调用无效,但在明确调用时有效如下:
FuncCaller<Int32>(i => MyFunc(i)) // valid
Run Code Online (Sandbox Code Playgroud)
我认为这是与拳击和拆箱类型相关的某种问题?
请参阅下面的代码
1. persons = Items.Select(item => componentResolver.ResolvePerson(new TridionUri(item.Id))).ToList();
2. persons.Each(person => person.AdditionalInfo); // gives null reference exception
Run Code Online (Sandbox Code Playgroud)
ResolvePerson看起来像:
public Person ResolvePerson(TridionUri personUri)
{
Person person = publicationResolverService.GetPerson(personUri);
if (author != null)
{
person.Id = personUri.ItemId.ToString();
}
return person;
}
Run Code Online (Sandbox Code Playgroud)
现在的问题是,"项目"包含一个人不存在的ID.所以我的'ResolvePerson'返回null.这导致第2行中的例外.我只能控制ResolvePerson方法.有没有办法可以跳过没有人在场的personUri而不是返回null?
刚开始并需要所有的帮助.以下代码将无法运行.错误消息msg表示"引用未设置为对象的实例",它指向WriteLine方法中的employee引用.请帮助
class Program
{
static void Main(string[] args)
{
List<Employee> empList = new List<Employee>()
{
new Employee { ID = 101, Salary = 6000000, Name = "Jane" },
new Employee{ ID = 102, Salary = 6000000, Name = "Jane" },
new Employee { ID = 103, Salary = 6000000, Name = "James" },
new Employee{ ID = 104, Salary = 6000000, Name = "Jasmie" },
new Employee { ID = 105, Salary = 6000000, Name = "Janet" },
};
Predicate<Employee> …Run Code Online (Sandbox Code Playgroud) 我正在研究程序的一部分(关于语音识别和遥控车),其中代码transmit(XXXXX); disableAutoMode();重复多次.为了好奇,我想将其转换成一个类似的lambda函数var f = p -> transmit(p); disableAutoMode();(原谅var,我不知道这个表达式的类型是什么),然后把它在一个类似的方式:f("s");,f("a");和f("f");或类似的东西到f.call("s");,f.call("a");和f.call("f");.
在Java中使用简单的lambda函数的正确语法是什么,类似于我上面描述的?(我应该放下什么类型而不是说var?)
如果你很好奇,这是代码块:
@Override
public void onResult(Hypothesis hypothesis) {
if (hypothesis != null) {
String text = hypothesis.getHypstr();
Log.i("onresult",text);
ToastMaster(text);
switch (text) {
case "forward":
case "go forward":
transmit("f");
disableAutoMode();
break;
case "go back":
case "go backward":
case "back":
case "backward":
case "reverse":
transmit("b");
disableAutoMode();
break;
case "skid left":
case "go left":
transmit("l"); …Run Code Online (Sandbox Code Playgroud) 我有以下代码,取自本MSDN:
public class First { }
public class Second : First { }
public delegate First SampleDelegate(Second a);
// Matching signature.
public static First ASecondRFirst(Second first)
{ return new First(); }
// The return type is more derived.
public static Second ASecondRSecond(Second second)
{ return new Second(); }
// The argument type is less derived.
public static First AFirstRFirst(First first)
{ return new First(); }
// The return type is more derived
// and the argument type is …Run Code Online (Sandbox Code Playgroud)