【c# – Generics / Delegates和带参数的函数】教程文章相关的互联网学习教程文章

c# – 实例方法的’this’参数可以是无类型的(即System.Object)吗?【代码】

我正在使用System.Reflection.Emit的TypeBuilder来发出一堆带有实例方法的自定义.NET类.例如:public class EmittedClass {public bool TryGetName(out string value){...}public bool TryGetAge(out int value){...} }所有方法都遵循可由通用委托描述的相同签名:public delegate bool TryGetter<T>(out T value);当然,我希望能够在调用站点上显式指定目标实例,如下所示:var instance = InstanceFactory.CreateInstance(); var t...

c# – Reflection.Emit使用参数创建对象【代码】

我正在创建一个动态函数,在给定构造函数params的object []的情况下在运行时创建一个对象.我一直得到通用异常’操作可能会破坏运行时’,我看不出我做错了什么. 如果创建的对象不需要构造函数参数,则该方法可以正常工作 – 因此问题必须在for循环中的代码中. 代码索引到给定的对象[]中,将对象放入堆栈,然后调用ctor并返回对象. 有任何想法吗???internal static Func<object[], object> CreateObjectFactoryMethodWithCtorParams(C...

c# – Rhino Mocks – 使用ref / out参数模拟集合【代码】

我还在学习犀牛嘲笑并对此有疑问.例如 – 我在mocked界面中有一个函数:public interface ISomeObject{string Name {get; set;}int Id {get;set;}}// This class will be returned as and answer to function callpublic class AnswerObject{public bool IfError {get;set;}}// Main interfacepublic interface IClass{AnswerObject FunctionGetCollection(ref ICollection <ISomeObject> ListOfInternalObjects, ref int Number);...

c#无法在没有有效操作参数的情况下处理请求.请提供有效的肥皂行动【代码】

我收到了这个错误.当我使用HttpWebRequest调用c#WebService时.System.Web.Services.Protocols.SoapException: Unable to handle request without a valid action parameter. Please supply a valid soap action.从wsdl我发现肥皂动作,我在请求中添加,仍然显示错误HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:33333/AccountService.asmx"); request.Method = "POST"; request.ContentType = "te...

c# – 在执行之前传递lambda表达式的方法参数类型【代码】

我试图将一些基于接口的抽象改进为遗留代码,作为依赖注入的初步步骤.遗留代码包含我正在努力封装的lambda用法.这是现有的lambda用法:private void MethodAaa(EntityA a, EntityB a, int someInt) {...}private void MethodBbb(DateTime date, EntityA e) {...}_commandObjectFromThirdPartyLibrary.Execute(() => MethodAaa(a, b, c));_commandObjectFromThirdPartyLibrary.Execute(() => MethodBbb(d, e));我希望通过公共基类方法...

从C#应用程序将命令行参数传递给IronPython?【代码】

如何将命令行参数从我的C#应用??程序传递到IronPython 2.x? Google仅返回有关如何使用Iron Python 1.x执行此操作的结果.static void Main(string[] args) {ScriptRuntime scriptRuntime = IronPython.Hosting.Python.CreateRuntime();// Pass in script file to execute but how to pass in other arguments in args?ScriptScope scope = scriptRuntime.ExecuteFile(args[0]); }解决方法:您可以通过以下C#代码设置sys.argv:stati...

c# – 参数化字符串以及LIKE和通配符运算符【代码】

我在搜索中看到SQL查询中参数化字符串的使用形式如下:SqlCommand comm = new SqlCommand(); comm.CommandText="SELECT * FROM table WHERE field LIKE '%'+@var+'%'"; comm.Parameters.AddWithValue("var","variabletext"); SqlDataReader reader = comm.ExecuteReader();然而,在这个论坛中提到它尽管在参数化字符串中使用,但仍受sql注入.我只能假设连接的字符串绕过所有参数化的安全性,只是将值直接作为字符串插入.如果是这种情况...

c# – GetHashCode()方法应该注意作为参数给出的null值吗?【代码】

在一些C#代码中,我使用linq GroupBy< TSource,TKey>()方法和自定义IEqualityComparer< T>.GroupBy(x => x.SomeField, new FooComparer());我用作分组键的字段可以为空.因此,我必须在Equals()方法中添加一些空检查:public bool Equals(Foo x, Foo y) {if (x == null && y == null)return true;else if (x == null && y != null)return false;else if (x != null && y == null)return false;else return x.Id == y.Id; }问题是:我应...

在C#中的单元测试中设置命令行参数

是否有可能在VS2012的单元测试中设置命令行参数?如果没有,除了编写批处理文件之外,还有其他方法可以测试这些参数的许多组合吗? 编辑:我有一个控制台程序,它读取和解析传递的命令行参数.我想确保程序具有正确的行为,无论传递什么命令行参数.我不是一遍又一遍地尝试所有组合,而是想编写一个单元测试来设置参数并用它们启动我的程序. 我没有使用任何特定的测试框架.只是通过创建一个新的测试项目在Visual Studio中提供的那个.解决方...

c# – 添加参数的问题【代码】

SqlParameter param = new SqlParameter(); param.ParameterName = "@name"; param.Value = tableName; SqlCommand cmd = new SqlCommand("select * from @name", con); cmd.Parameters.Add(param); SqlDataReader rd = cmd.ExecuteReader();上面的代码导致以下错误消息:Must declare the table variable “@name”.为什么我会收到此错误以及如何解决?解决方法:错误的查询select * from @name请更正它,在From table或view name之...

c# – 在.Select()中将Func中的匿名作为参数返回【代码】

为了方法的“后处理”,我想将一个额外的函数导入到一个方法中. 如何导入一个返回匿名类型的Func作为.Select扩展方法的参数? 表达方式如下:p => new {ThumnailUrl = p.PicasaEntry.Media.Thumbnails[0].Attributes["url"],ImageUrl = p.PhotoUri }并且需要在参数?????中使用并在.Select(?????)执行private void BindControl<T, U>(string uri, DataBoundControl target, ?????)where T : KindQuery, new()where U : PicasaEntity,...

C#中带“out参数”的递归【代码】

我可以使用递归方法的out参数吗?如果可能,我如何使用以下代码执行此操作?private void PrepareDir(out List<_File> listFiles,string root,string dirPath) {DirectoryInfo dirRoot = new DirectoryInfo(dirPath);FileInfo [] Files = dirRoot.GetFiles();dirPath = dirPath.Substring(root.Length);foreach (FileInfo file in Files) {_File _file = new _File();_file.Name = dirPath + "\\" + file.Name;_file.Path = file.Fu...

c# – 无效的回发或回调参数.只获取图像按钮【代码】

我收到以下错误:Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the C...

c# – 引入Linq后接受IEnumerable作为参数是否安全?【代码】

有一些类似的问题涉及正确的输入和输出类型like this.我的问题更多的是关于正确的实践,方法命名,选择参数类型,防止事故等在Linq之后. Linq几乎无处不在处理IEnumerable而这只是不是它,但它也引入了一些我们称之为延迟执行的异类.现在,当我们认为最好的想法是采用最基本的类型时,我们可能在设计我们的方法(特别是扩展方法)时出错了.所以我们的方法看起来像:public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> lstObject...

c# – 确定方法参数的上下文【代码】

这是一个具有挑战性的问题.是否可以使用任何方法隐式确定作为参数传递给方法的属性的名称? (这可能起初看起来像是另一个问题的副本,但是有一个微妙但重要的不同,因为我们总是使用属性,这是关键). 以下是示例场景:public class Foo{public string Bar { get; set; }}public void SomeStrangeMethod(){Foo foo = new Foo() { Bar = "Hello" };string result = FindContext(foo.Bar); // should return "Bar"}public string FindCo...