【是否可以限制C#中可选参数的值?】教程文章相关的互联网学习教程文章

c# – 从一个公共类调用静态参数到另一个公共类【代码】

我正在尝试编写一个多线程应用程序,我需要使用Monitoer.Enter / Exit / Wait / Pulse我已经创建了一个Lock对象,并在它自己的类中使用它public partial class domain /*I dont sure this is the good practice way to mange DDD Layers (if anybody have a comment about it)*/ {Public class Peer2PeerCom{public static readonly object locker = new object();//other stuff here//...//somwhere hereMonitor.Pulse(locker);} ...

c# – 如何在WCF服务中将Interface作为参数传递?【代码】

我有以下但是我不确定这是正确的做法.namespace WCFServices {[ServiceContract(Name = "IService")][ServiceKnownTypeAttribute(typeof(DataItem))]public interface IService{[OperationContract]void InstantiateThirdParties(string name, IEnumerable<IDataItem> data, IEnumerable<string> modules, IEnumerable<string> states);} }这是使用该接口的代码.namespace WCFServices {public class Service : IService{public vo...

c# – LINQ选择新模型而无需键入每个参数【代码】

是否有可能在LINQ中将两个表中的所有属性都获取到新的ViewModel而不列出PersonId,Email等.var person = (from o in db.Personsjoin od in db.OsobeDetails on o.PersonId equals od.PersonIdwhere o.PersonId == idselect new PersonViewModel{PersonId= o.PersonId,Email = o.Email}).ToList();就像是:... select new PersonViewModel(); 我尝试了这个,但我没有得到任何结果. “id”是方法的输入参数.解决方法:您可以覆盖PersonV...

c# – 除了可选参数之外,为什么允许有2个相同的方法签名?【代码】

public static bool TryGetDbRow(DbConnection cnctn, string indexName = null)public static bool TryGetDbRow(DbConnection cnctn)没有字符串作为最后一个参数,将不会调用第一个.除了可选参数之外,为什么允许使用2个相同的方法签名?解决方法:Eric Lippert在他的博客http://blogs.msdn.com/b/ericlippert/archive/2011/05/16/optional-argument-corner-cases-part-three.aspx上解释了这一点 使用可选参数创建方法时,方法签名包括...

c# – 使用typeof作为泛型类型参数【代码】

根据typeof的文档:[…] To obtain the run-time type of an expression, you can use the .NET Framework method GetType因此,这意味着typeof必须是编译时表达式. 在关于Generic Type Parameters的文章中,声明:[…] The type argument for this particular class can be any type recognized by the compiler.编译器识别的任何类型都是编译器推断的任何类型,即编译时已知的任何类型. 如果这是真的,那么为什么不允许以下语句?int...

c# – 将依赖项作为方法参数而不是构造函数参数注入【代码】

我们可以使用MEF或Autofac将依赖项作为方法参数而不是构造函数参数注入吗? 谢谢解决方法:我不知道MEF,因为我从未使用它.你可以do it with Unity和with Autofac 统一 从MSFT文档.Unity instantiates dependent objects defined in parameters of methods that carry the InjectionMethod attribute within the scope of the target object. Then it calls the attributed method of the target object before returning the object...

c# – 为什么元组在`[Serializable]`时没有无参数构造函数?

标有[SerializableAttribute]的System.Tuple(T1,..)类(参见msdn).但它们没有无参数构造函数,因此XmlSerializer不能序列化,因为它在here之前已被清除.那么为什么不添加一个呢?解决方法:你不需要一个类型来使用[Serializable]属性来使用XmlSerializer来序列化它,并且一个类型具有该属性的事实并不意味着你可以使用XmlSerializer序列化它;这两者是无关的. [Serializable]仅用于二进制序列化(*)(不需要无参数构造函数). 把它们加起来:...

c# – 更改参数名称而不破坏向后兼容性【代码】

我正在开发一个c#库,所以我们关注的是打破向后兼容性,但我想知道是否可以更改参数的名称并保持向后兼容性,因为能够使用命名参数?我想要做的一个例子如下[Obsolete("use ChangeSpecificFoo(SpecificFoo specificFoo)")] public void ChangeSpecificFoo(SpecificFoo foo) {_specificFoo = foo; }//Compile error ... already defines a member called 'ChangeSpecificFoo' with the same parameter types public void ChangeSpecifi...

C#在路径中使用带参数和空格的Process.Start【代码】

我见过类似的例子,但找不到与我的问题完全相同的东西. 我需要从C#运行这样的命令:ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = "cmd"; startInfo.Arguments = "/K D:\\as df\\solver\\Swag.Console.exe -f D:\\as df\\solver\\2035.swag -p 5555"; Process.Start(startInfo);不起作用.startInfo.Arguments = "/K \"D:\\as df\\solver\\Swag.Console.exe\" -f D:\\as df\\solver\\2035.swag -p 555...

c# – 将参数传递给UnityEvent【代码】

好的,我有点困惑.我通过this教程学习了UnityEvent和Messaging System.但我有一个问题,我无法理解.如何将参数传递给Invoking函数? 例如(我在教程中有EventManager):void OnEnable() {EventManager.StartListening ("OnPlayerTeleport", TeleportPlayer);}void OnDisable() {EventManager.StopListening ("OnPlayerTeleport", TeleportPlayer);}void TeleportPlayer () {float yPos = transform.position.y;yPos += 20.0f;transfor...

将表达式和lambdas参数传递给F#中的C#扩展方法【代码】

我正在努力解决这个问题,因为我只是F#的初学者,目前我正在学习F#,通过在F#中为C#代码创建单元测试,如F#所示,以获得乐趣和利润. 问题. 我有以下C#类public class ObjectMapper<TSource, TTarget> where TTarget : class, new() {public readonly TSource Source;public readonly TTarget Target;public ObjectMapper(TSource source) {this.Source = source;this.Target = new TTarget();}public ObjectMapper<TSource, TTarget> Po...

c# – 如果该序列不为空,则使用IEnumerable序列作为参数调用方法【代码】

我有方法Foo,它进行一些CPU密集型计算并返回IEnumerable< T>序列.如果该序列为空,我需要检查.如果没有,请使用该序列作为参数调用方法Bar. 我想到了三种方法…… >使用Any()检查序列是否为空.这是好的,如果序列真的是空的,大多数时候都是这种情况.但它会有可怕的性能,如果序列将包含一些元素,Foo将需要它们再次计算…>将序列转换为列表,检查该列表是否为空…并将其传递给Bar.这也有局限性.酒吧只需要前x个项目,所以Foo将做不必要的工...

c# – 将多个参数从IronPython传递给.NET方法【代码】

我有一个.NET(C#)类:public class MyHelper {public object exec( string script, params object[] arguments ) {// execute script with passed arguments in some external enviroment} }我在我的代码中使用IronPython运行时来运行python脚本,在某些情况下应该调用“exec”方法.我想以舒适的方式调用“exec”方法.就像是:helper.exec( "someExternalFunction( {0}, {1}, {3} )", var01, var02, var03 )但我不知道如何在C#中声明...

C# – 在参数中声明方法(委托)【代码】

在下面的代码中,我将方法B作为要在IterateObjects方法中的对象上执行的操作传递.我想问一下我是否可以在参数中显式声明方法,而不是按名称传递它,如下所示:a.IterateObjects(委托void(string s){//方法体})它不正确,但我确信我已经看到了类似的工作.您能否提一些建议?谢谢DelTest a = new DelTest(); //class with method IterateObjects a.IterateObjects(B) //HEREprivate void B(string a) {listBox1.Items.Add(a); }//another...

c# – Web服务请求调用SOAP请求缺少空参数【代码】

我是Web服务和C#的新手,所以如果我的问题太简单,请原谅我.我已经四处搜索但找不到答案 – 至少有一个基于我的关键字. 我试图通过C#(Visual Web Developer 2010 Express)调用Web服务,但我收到错误作为响应.当我通过soapUI调用相同的Web服务时,我没有收到错误.当我将来自C#的SOAP请求与来自soapUI的SOAP请求进行比较时,C#SOAP请求缺少故意为空的参数< aclObjectId>.当我从soapUI请求中取出空参数时,我得到了同样的错误.对我来说,答案...