【虚方法和重写方法中的c#可选参数】教程文章相关的互联网学习教程文章

c# – 使用多个泛型类型参数进行类型推断【代码】

我不明白为什么C#在以下完整情况下不会推断出类型:public interface IThing {}public class Thing1 : IThing {}public class Thing2 : IThing {}public interface IContainer {}public class Container1 : IContainer {public IThing A { get { return new Thing1(); } }public IThing B { get { return new Thing2(); } } }public class Container2 : IContainer {public IThing C { get { return new Thing1(); } }public IThin...

c# – 确定类是否是具有多个泛型参数的类型的子类【代码】

给定以下类层次结构public abstract class MyGenericClass<T1, T2> {public T1 Foo { get; set; }public T2 Bar { get; set; } }public class BobGeneric : MyGenericClass<int, string>{} public class JimGeneric : MyGenericClass<System.Net.Cookie, System.OverflowException>{}我原以为我可以做到以下几点//All types in the assembly containing BobGeneric and JimGeneric var allTypes = _asm.GetTypes(); //This works...

c# – 对Func的协变分配需要显式参数【代码】

您可以将方法分配给具有匹配类型args的委托:Func<string, DateTime> f = DateTime.Parse;您可以将lambda分配给具有协变类型args的委托:Func<string, object> f = s => DateTime.Parse(s);但是您不能将方法分配给具有协变类型args的委托:Func<string, object> f = DateTime.Parse; //ERROR: has the wrong return type为什么不?解决方法:方差不适用于值类型,因为它们需要以不同方式进行JIT. 您的lambda表达式变体不涉及方差;相反...

c# – 为什么我的委托作为参数传入方法时不起作用?【代码】

我创建了一个委托,我想使用委托作为方法参数列表中的参数.我想要像在Main方法中那样完美地调用处理程序. 问题:如何将委托传递给方法? 码:using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace ConsoleApplication1 {class Program{public delegate void Del(string e);Del handler = DelegateMethod;public static void DelegateMethod(string message){System.Console.WriteLine(...

c# – 如何使MVC接受参数中的点?【代码】

参见英文答案 > Dots in URL causes 404 with ASP.NET mvc and IIS 17个我有以下路由配置:routes.MapRoute(name: "Downloads",url: "downloads/{filename}",defaults: new { controller = "Downloads", action = "Index", filename = UrlParameter.Optional });并遵循控制器代码:public ActionResult Index(string filename){ ...当我用http://test.com/downloads/test.txt调用此Action时,我...

c# – 当DropDownList选项更改时,使用参数重定向到MVC ActionResult【代码】

我正在使用MVC创建网站的一部分.在我的一个视图中,我有一个DropDownList.当选择一个新的下拉列表选项,或者换句话说onchange时,我希望我的页面被重定向到特定的Controller ActionResult.如果没有参数,我可以访问MyAction ActionResult但是我无法弄清楚如何发送所需的参数. 我的控制器动作:public virtual ActionResult MyAction(int param1, int param2) {return View(); }我在视图中的DropDownList:@Html.DropDownList("viewData...

c# – 使用NInject在WPF中注入没有无参数构造函数的viewmodel类【代码】

我正在使用NInject来解析我的第一个WPF应用程序的依赖项.以下是我的代码片段. 我的App.xaml.cs就像.public partial class App : Application {private IKernel container;protected override void OnStartup(StartupEventArgs e){base.OnStartup(e);ConfigureContainer();ComposeObjects();}private void ComposeObjects(){Current.MainWindow = this.container.Get<MainWindow>();}private void ConfigureContainer(){this.contai...

c# – ASP.NET WebAPI 2:如何在URI中将空字符串作为参数传递【代码】

我在ProductsController中有这样的函数:public IHttpActionResult GetProduct(string id) {var product = products.FirstOrDefault((p) => p.Id == id);return Ok(product); }当我使用此URL发送GET请求时:api/products?id=它将id视为null.如何将其视为空字符串?解决方法:这个public IHttpActionResult GetProduct(string id = "") {var product = products.FirstOrDefault((p) => p.Id == id);return Ok(product); }或这个:pub...

c# – NSubstitute无法确定要使用的参数规范【代码】

我使用NUnit和NSubstitute进行单元测试.我有以下内容:public interface IDataProvider {void Log(int tvmId, DateTime time, int source, int level, int eventCode, string message); }...var fakeDataProvider = Substitute.For<IDataProvider>(); ... fakeDataProvider.Received().Log(Arg.Any<int>(),new DateTime(2000, 1, 1),0,0,0,null);fakeDataProvider.Received()抛出AmbiguousArgumentException,并显示无法确定要使用的...

c# – 作为方法调用参数的函数【代码】

我有一个简单的问题,可能很容易回答,但谷歌的强烈使用没有提出我的问题的答案.所以我道歉,如果有正确的解决方案,我没有看到它. 如果我有一个类似的方法调用Object.Add(string text, System.Drawing.Color color);这是一些文本添加到具有指定颜色的某个对象,我想动态地改变颜色,然后我可以输入某个.喜欢Object.Add("I'm a string", SomeBool ? Color.Red : Color.Green);这非常有用,但只要我想比较两个案例就会失败. 我正在寻找的东...

C# – 如何使用数组数组作为“参数化”方法的输入?【代码】

我想创建一个方法,接受字符串数组的字符串数组类型的“参数化”输入对象.就像是:public void MyMethod(params string[][] input) {//...do stuff }我将此方法称为如下:MyMethod({"arry1-elem1","arry1-elem2"}, {"arry2-elem1","arry2-elem2"}, {"arry3-elem1","arry3-elem2"});但是,当我这样做时,我收到以下错误:Invalid expression term ‘{‘我在这做错了什么.是否无法输入隐式类型数组作为输入?解决方法: MyMethod(new str...

C# 函数参数中的this【代码】

先看下面的代码:public static class StringExtension {public static void Foo(this string s){Console.WriteLine("Foo invoked for {0}", s);} }为什么这里会有一个this关键字,做什么用?其实这就是扩展方法!这个扩展方法在静态类中声明,定义一个静态方法,其中第一个参数定义可它的扩展类型。Foo()方法扩展了String类,因为它的第一个参数定义了String类型,为了区分扩展方法和一般的静态方法,扩展方法还需要给第一个参数使...

c# – 将参数或参数传递给Uwp中的后台任务【代码】

我正在创建一个uwp应用程序,我想从用户说从文本框中获取一些数据,然后将其传递给后台任务.但是当我试图将项目引用添加到后台任务时,我得到一个循环引用错误.那么有没有办法传递参数可能是运行函数或其他任何东西的重载.提前致谢.解决方法:Romasz已经完美地解释了它,但在您的情况下,您可以通过以下步骤从文本框中获取用户数据: 1.在MainPage.xaml.cs(或你的xaml页面)中声明这个var localSettings = Windows.Storage.ApplicationDat...

c# – 使用Reflection.Emit实例化具有通用参数的通用类型【代码】

我的目标是使用反射发射来构造泛型类型,其中包含创建的泛型方法的泛型参数所以创建的泛型方法的最终结果类似于void DoSomeThing<T>(T arg){ var list=new List<T>(); }所以我需要的是用于发出这段代码的代码new List<T>这是我的尝试var _assemblyName = "asm.dll";var _assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName(_assemblyName), System.Reflection.Emit.AssemblyBuilderAccess.RunAndSa...

c# – WPF,MVVM和PRISM – 为此对象定义的无参数构造函数【代码】

回答好的,所以添加E-Bat给出的建议代码没有任何影响,直到我开始一个新项目并逐字复制所有代码.我只能假设在http://prismlibrary.com/上的ViewModelLocator中必须有一些后台代码,它们没有更新以考虑无参数构造函数.希望这可以帮助其他人解决同样的问题 原始问题我使用棱镜设置了一个MVVM项目.我有一个MainWindow.xaml和5个视图;我正在使用的ButtonsView,HeaderView,ProcessInputView,ProcessLogView和ProcessSelectionView,每个View...