Tag: 行为

编译器琐事:这段代码的结果是什么

我今天正在审查一些代码并遇到了一些代码(由此代码段准确描绘)…… public abstract class FlargBase{ public FlargBase(){ this.DoSomething(); } public abstract void DoSomething(); } public class PurpleFlarg: FlargBase{ public PurpleFlarg() : base(){ } public override void DoSomething(){ // Do something here; } } 编译器不提供任何错误或警告,但CodeAnalysis警告调用链包含对虚方法的调用,并可能产生意外结果。 我很好奇,因为正如我所看到的,有两件事情可能发生。 创建基类的实例将调用没有定义实现的方法。 我希望编译器出错,或者由于缺少实现而导致运行时抛出exception。 我假设编译器提供了{}的实现 我错误输入了原始代码; 它确实在类中包含了abstract关键字。 创建派生类的实例将导致对尚未实际构造的类的方法进行调用。 我原以为这会抛出exception。 此代码已在生产环境中使用了几个月。 它显然工作正常,没有人注意到任何奇怪的行为。 我希望StackOverflow上的不可思议的人才可以让我对这段代码的行为和后果有所了解。

使用IParameterInspector.BeforeCall(字符串operationName,object 输入)中止调用

我有一个自定义行为,我实现“IParameterInspector”,以便能够使用BeforeCall和AfterCall。 我正在使用这些方法在我的WCF服务上执行调用之前进行一些validation。 这是我的属性: [AttributeUsage(AttributeTargets.Class)] public class SendReceiveBehaviorAttribute : Attribute, IServiceBehavior { public SendReceiveBehaviorAttribute() { } public void ApplyDispatchBehavior(ServiceDescription desc, ServiceHostBase host) { foreach (ChannelDispatcher cDispatcher in host.ChannelDispatchers) { foreach (EndpointDispatcher eDispatcher in cDispatcher.Endpoints) { foreach (DispatchOperation op in eDispatcher.DispatchRuntime.Operations) { op.ParameterInspectors.Add(MyInspector); } } } } public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection endpoints, BindingParameterCollection bindingParameters) […]

当子类已经从不同的基类派生时,子类如何共享行为?

我有两个实现ISomeBehavior的类。 现在我希望他们分享function。 通常我会用一个抽象类替换ISomeBehavior,比如SomeBehaviorBase。 问题是其中一个子类已经从另一个类派生,而另一个类不是我们拥有的软件。 (这是C#,因此多重inheritance不是一种选择。)从第三方类派生的子类没有实现。 它只是派生自第三方类,并实现了ISomeBehavior,因此第三方类的处理方式与实现ISomeBehavior的其他子类相同。 我目前所做的是在ISomeBehavior上实现一个扩展方法。 现在消费代码可以调用该方法。 这种方法的问题是我想强制调用代码使用这个扩展方法。 我无法从界面中删除SomeMethod(),因为扩展方法最终必须调用它。 关于如何让两个类优雅地分享相同的行为的任何想法,当其中一个已经派生自另一个,第三方,类? 注意:策略设计模式在这里听起来很有意义,但是当子类之间的行为发生变化时会使用该模式。 这里的行为没有变化; 它只需要分享。

为什么Thread.Sleep在运行其他应用程序时等待的时间比请求的长?

关于C#中的线程,我有一个小问题。 出于某种原因,当我打开Chrome时,我的线程从32ms延迟加速到16ms延迟,当我关闭Chrome时它会回到32ms。 我正在使用Thread.Sleep(1000 / 60)来延迟。 有人可以解释为什么会这样,并可能建议一个可能的解决方案? using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace ConsoleApplication2 { class Program { static bool alive; static Thread thread; static DateTime last; static void Main(string[] args) { alive = true; thread = new Thread(new ThreadStart(Loop)); thread.Start(); Console.ReadKey(); } static void Loop() { last = DateTime.Now; while […]

为什么Enum.Parse会创建未定义的条目?

class Program { static void Main(string[] args) { string value = “12345”; Type enumType = typeof(Fruits); Fruits fruit = Fruits.Apple; try { fruit = (Fruits) Enum.Parse(enumType, value); } catch (ArgumentException) { Console.WriteLine(String.Format(“{0} is no healthy food.”, value)); } Console.WriteLine(String.Format(“You should eat at least one {0} per day.”, fruit)); Console.ReadKey(); } public enum Fruits { Apple, […]

具有行为的RadioButtons绑定到单个属性

我有几个RadioButtons,我不想将它们中的每一个的“IsChecked”属性绑定到代码中的唯一属性。 我希望有一个像“CurrentSelected”这样的属性,并根据它来设置“IsChecked”。 另外我不想使用转换器。 我试图使用行为“ChangePropertyAction”,但看起来它只是以一种方式工作。 这是我的代码: 我的视图模型非常简单:MainViewModel.cs public class MainViewModel : ViewModelBase { private DirectionEnum _selectedDirection; public DirectionEnum SelectedDirection { get { return _selectedDirection; } set { if (_selectedDirection != value) { _selectedDirection = value; RaisePropertyChanged(); } } } public MainViewModel() { SelectedDirection = DirectionEnum.Up; } } 从代码中可以看出,应该已经检查了“Up”RadioButton ……我错过了什么?