Tag: 动作

将代码作为参数传递给方法

我有一个方法列表几乎完全相同,除了一些差异: void DoWork(string parameter1, string parameter2) { //Common code … //Custom code … //Common code … } 我想通过从另一个方法传递自定义代码来重用公共代码来简化解决方案。 我假设我必须使用带参数的动作来完成这个,但无法弄清楚如何。

如何使用FakeItEasy伪造一个动作

我正在使用FakeItEasy库为我的unit testing创​​建假货。 我有一个ClassUnderTest ,我想在其上测试方法MethodToTest(Data dataObject) 。 这个方法调用一个我想伪造的接口的方法: public interface IFoo { void Execute(Action action); } public class ClassUnderTest { private IFoo _foo; public ClassUnderTest(IFoo foo) { _foo = foo; } public void MethodToTest(Data dataObject) { _foo.Execute(dataAccess => dataAccess.Update(dataObject)); } } public interface IDataAccess { void Update(Data data); } public class Data { public int Property […]

动作属性

任何人都可以解释我这段代码的提取。 public abstract Action serialpacket { set; get; } 我有点困惑。 我大致知道它的作用,但如果有人能够对它有所了解会更好。

C#在X秒后执行动作

我想开发一个Windows控制台应用程序,它在给定时间后定期执行一个动作。 我已经读过某个计时器类只适用于Windows窗体应用程序,那么实现我想要的最佳方法是什么?

ASP.NET Core中的自定义授权属性

我正在研究asp.net核心,我不了解一些事情。 例如,在mvc.net 5中,我们可以使用AuthorizeAttribute中的create class过滤和授权操作,并将属性设置为这样的操作: public class AdminAuthorize : AuthorizeAttribute { public override void OnAuthorization(AuthorizationContext filterContext) { base.OnAuthorization(filterContext); if (filterContext.Result is HttpUnauthorizedResult) filterContext.Result = new RedirectResult(“/Admin/Account/Login”); } } 但是在asp.net核心中我们没有AuthorizeAttribute …如何在asp.net核心中为自定义操作设置这样的filter?

ASP.NET MVC – 来自控制器代码的当前动作?

这与最近的另一个问题非常相似: 如何在ASP.NET MVC视图中返回当前操作? 但是,我想从控制器代码中获取当前操作的名称。 因此,在Action调用的函数代码中,我想获取当前Action名称的字符串。 这可能吗?

带有2个动作的单行if语句

我想做一行if语句有多个动作。 默认是这样的: (if) ? then : else userType = (user.Type == 0) ? “Admin” : “User”; 但我不需要“别的”,我需要一个“别的如果” 像多行中那样: if (user.Type == 0) userType = “Admin” else if (user.Type == 1) userType = “User” else if (user.Type == 2) userType = “Employee” 单线有可能吗?

Action委托使用在foreach循环外声明的变量的最后值

我有这段代码: int i = 0; foreach(var tile in lib.dic.Values) { var ii = i; var t = tile; Button b = new Button( () = > { MainStatic.tile = t; } ); Checkbox c = new Checkbox( () = > { lib.arr[ii].b = !lib.arr[ii].b; } ); i++; } 虽然上面的代码可以正常工作,但下面的代码如下: int i = 0; foreach(var tile […]

在调用新Action时帮助理解C#语法

我是c#的新手,并不了解调用新操作的语法,甚至不了解操作是什么。 根据我对Port1_DataReceived的理解,我必须创建一个动作,因为我正处于新的阶段……任何人都可以详细说明为什么我需要这样做? public Form1() { InitializeComponent(); SerialPort Port1 = new SerialPort(“COM11”, 57600, Parity.None, 8, StopBits.One); Port1.DataReceived += new SerialDataReceivedEventHandler(Port1_DataReceived); Port1.Open(); } private void Port1_DataReceived(object sender, SerialDataReceivedEventArgs e) { SerialPort Port = (SerialPort)sender; string Line = “”; int BytestoRead = Port.BytesToRead; Line = Port.ReadLine(); label1.Invoke(new Action(() => { label1.Text = Line; })); } 我真正理解的代码片段是: label1.Invoke(new Action(() […]

从Action 获取参数

如何将参数传递给Action ? 代码示例应该突出我想要实现的目标。 对不起,这有点长。 class Program { static void Main(string[] args) { Foo foo = new Foo(); foo.GetParams(x => x.Bar(7, “hello”)); } } class Foo { public void Bar(int val, string thing) { } } static class Ex { public static object[] GetParams(this T obj, Action action) { // Return new object[]{7, “hello”} } } […]