【c#-隐式键入的参数】教程文章相关的互联网学习教程文章

c#-从收到的调用中提取参数并对其进行断言【代码】

如何从收到的呼叫中对参数进行断言?下面的示例不起作用,因为从未调用传递给Arg.Do()的操作.IEnumerable<Tuple<string, string>> receivedlArgs = null; provider.Received(1).SetValuesAsync(Arg.Do<IEnumerable<KeyValuePair<string,object>>>(args =>{receivedlArgs = args.Select(a => new Tuple<string, string>(a.Key, a.Value.ToString()));}));// assert (using FluentAssertions - but just for the example) receivedlAr...

C#-RowHeaderTemplateSelector对象参数为null【代码】

嗨,我正在使用WpfToolKit DataGrid并想根据项目类型动态设置RowHeaderTemplate,在我的代码中object参数始终为null这是我的代码 a<DataTemplate x:Key="WithCheckBox"><Grid><CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type wpftk:DataGridRow}}}"/></Grid></DataTemplate><viewModel:CheckBoxRowDataTemplate x:Key="CheckBoxRowDataTemplate"/>...

在C#中使用setter作为参数? (也许有代表吗?)【代码】

我们正在为某些硬件开发API,而我正在尝试用C#为其编写一些测试.重复任务的try-catch块使我的代码变得肿且重复,因此对于吸气剂,我能够像这样包装:TestGetter(Func<int> method, double expectedVal) {int testMe = 0;try{testMe = method();PassIfTrue(testMe == expectedVal);}catch (Exception e){Fail(e.Message);} }因此,我向硬件查询一些已知值并进行比较.我可以打电话给:TestGetter( () => myAPI.Firmware.Version, 24); //...

c#-在url.action中传递带有隐藏参数的url【代码】

有什么办法可以隐藏参数并传递window.location.href = '@Url.Action("index", "mycntroller")?at='+119nuju解决方法:尽管您可以使用POST请求或使用coockies或请求标头或…来防止乍看之下显示参数,但是您应该知道隐藏参数永远不会有助于提高网站的安全性.因为可以在诸如Developer Tools,FireBug等工具中监视所有参数,所以它们在此处是完全可见的. 隐藏参数通常是为了获得更好的用户体验,而用户看不到对他没有意义的参数. 因此最好不...

c#-从类中获取泛型参数的类型,该类是从泛型接口继承的【代码】

我有此接口及其实现:public interface IInterface<TParam> {void Execute(TParam param); }public class Impl : IInterface<int> {public void Execute(int param){...} }如何使用typeof(Impl)的反射获取TParam(此处为int)类型?解决方法:您可以使用一些反射:// your type var type = typeof(Impl); // find specific interface on your type var interfaceType = type.GetInterfaces().Where(x=>x.GetGenericTypeDefinition() =...

在C#中应为通用接口基本类型时,如何传递方法参数?【代码】

假设我有一个定义如下的接口:interface IContract {void CommonMethod(); }然后是从该接口继承的另一个接口,该接口的定义方式如下:interface IContract<T> : IContract where T : EventArgs {event EventHandler<T> CommonEvent; }我的具体问题是,给定实现IContract的任何实例,如何确定IContract< T>如果是,则如何确定IContract< T>的通用类型是什么?无需对每个已知类型的IContract T进行硬编码.我可能会遇到. 最终,我将使用此确...

使用嵌套在通用类中的类作为C#中的类型参数【代码】

具有以下定义:public class Generic<T> {public class Nested { } }鉴于ECMA参考25.1指出:Any class nested inside a generic class declaration or a genericstruct declaration (25.2) is itself a generic class declaration,since type parameters for the containing type shall be supplied tocreate a constructed type.我知道Nested需要type参数才能实例化. 我可以使用typeof获得泛型Type:var type = typeof(Generic<>....

c#-参数为null时返回null的安全哈希码函数的合适值是什么?【代码】

这个问题已经在这里有了答案: > Should the hash code of null always be zero, in .NET 8个因此,我有一个具有大量属性的对象,因此,需要对它们进行比较,因此,我必须覆盖GetHashCode.令人头疼的是,任何属性都可以为空,所以我重复了几行int hashcode = 0; if (!String.IsNullOrEmpty(Property1)) hashcode ^= Property1.GetHashCode(); if (!String.IsNullOrEmpty(Proper...

c#-传递get / set属性作为参数而不进行反射的最佳方法【代码】

有时,在处理数据传输对象(例如从数据库或csv文件中检索到的对象)时,最好编写一些辅助函数来移动数据. 例如:class MyDto {public string Name { get; set; } }class MyBusinessObject {public string Name { get; set;} }我想写一些类似的东西:MyDto source; MyBusinessObject target;var hasChanged = target.Set(source, source => source.Name, target => target.Name); // lamdba expressions, or whatever it takes to make ...

c#-从文件中给出存储过程的输入参数【代码】

我有一个带有3个参数的存储过程,我想我没有正确执行存储过程的第一个参数.string path = @"D:\test2"; var ngd = Directory.EnumerateFiles(path, "*.txt").Select(file => File.ReadAllLines(file)).FirstOrDefault(); using (SqlConnection connection = new SqlConnection("Data Source=;Initial Catalog=;User ID=;Password=")) {using (SqlCommand cmd = new SqlCommand("usp_SaveData", connection)){try{await Task.Delay(1...

C#中在定义事件委托时怎样跨窗体传递参数【代码】

场景 C#中委托与事件的使用-以Winform中跨窗体传值为例: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100150700 参照上文在定义事件时传递参数为简单的string,如果要传递比较复杂的参数,那么就可以使用对象将参数进行封装。 博客主页: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 获取编程相关电子书、教程推送与免费下载。 实现 在窗体A中定义委托与事件public delegate void Refres...

从C#代码使用python.net调用具有命名参数的python函数【代码】

我想从C#代码中调用python函数.为此,我使用Python for .NET调用函数,如以下代码行所示using System;using Python.Runtime;public class Test{public static void Main(){using(Py.GIL()){dynamic lb = Py.Import("lb");dynamic result = lb.analyze("SomeValue");Console.WriteLine(result);}}}python函数是这样的:def analyze(source, printout = False, raw = True):# removed for bravity所以问题是,当我从C#代码中调用分析函数...

如何在C#中获取打开的泛型上类型参数的数量【代码】

简而言之,这几乎可以解释我的问题…class Foo<T> { ... } var type = typeof(Foo<>); <-- runtime provides a RuntimeType object instance in my real code var paramCount = ((RuntimeType)type).GetGenericParameters().Count; <-- I need this当然,问题在于“ RuntimeType”是mscorlib中的内部类型,因此我无法从代码中访问它. 还有另一种/更好的方法吗? 更新: 我找到了一种“丑陋且可能不安全”的方法来基本实现我所需要的,但...

C#-ProjectTo无法识别无参数构造函数【代码】

我有一个用于从数据库获取数据的类,它看起来像这样(为了简单起见,缺少一些字段):public class BranchDetailDto {public BranchDetailDto(){}public BranchDetailDto(int supplierId, string supplierName){SupplierId = supplierId;SupplierName = supplierName;}public int Id { get; set; }public int SupplierId { get; set; }public string SupplierName { get; set; } }然后,我想在查询中检索数据并为其使用AutoMapper的Proj...

C#将通用函数作为参数传递【代码】

我正在尝试从应用程序内部审核方法调用,以便可以分析和分析参数.我有这个工作,Run( ()=> SomeFunction(paramA, paramB), paramA, paramB) );public static void Run(Expression<Action> action, params object[] param) {var func = action.Compile();func(); }public static T Run<T>(Expression<Func<T>> action, params object[] param) {var func = action.Compile();return func(); }但是,我希望丢失lambda,并使该方法更易于使...