假设我有一个string,例如, string snip = "</li></ul>"; 我想基本上写多次,这取决于一些整数值。 string snip = "</li></ul>"; int multiplier = 2; // TODO: magic code to do this // snip * multiplier = "</li></ul></li></ul>"; 编辑:我知道我可以很容易地写我自己的函数来实现这一点,我只是想知道是否有一些奇怪的string运算符,我不知道
我正在尝试使用扩展方法将操作符重载添加到C# StringBuilder类。 具体来说,给定的StringBuilder sb ,我想sb += "text"等于sb.Append("text") 。 以下是为StringBuilder创build扩展方法的语法: public static class sbExtensions { public static StringBuilder blah(this StringBuilder sb) { return sb; } } 它成功地将这个扩展方法添加到了StringBuilder 。 不幸的是,运算符重载似乎不起作用: public static class sbExtensions { public static StringBuilder operator +(this StringBuilder sb, string s) { return sb.Append(s); } } 在其他问题中,关键字在这种情况下是不允许的。 是否可以通过扩展方法添加运算符重载? 如果是这样,那么有什么正确的方法呢?
这个方法是否被调用了一个空值,或者它是否给出一个空引用exception? MyObject myObject = null; myObject.MyExtensionMethod(); // <– is this a null reference exception? 如果是这种情况,我将永远不需要检查我的“这个”参数为空?
我正在听一个关于C#4 dynamic关键字的讨论,我想知道…这个特性是否与其他.NET特性是正交的,例如它是否支持扩展方法? public static class StrExtension { public static string twice(this string str) { return str + str; } } … dynamic x = "Yo"; x.twice(); // will this work? 注意:这个问题在C#4出货之前就已经被问到了,这就是为什么它会以将来的forms出现。
有没有什么可以用来确定一个types是否实际上是一个匿名types? 例如一个接口等? 目标是创build如下的东西… //defined like… public static T Get<T>(this IAnonymous obj, string prop) { return (T)obj.GetType().GetProperty(prop).GetValue(obj, null); } //… //And then used like… var something = new { name = "John", age = 25 }; int age = something.Get<int>("age"); 还是仅仅是一个匿名types的美丽? 没有什么可以识别自己,因为它需要一个新的形状? 注意 – 我意识到你可以为对象类写一个扩展方法,但在我看来,这似乎有点矫枉过正。
在维护一个严重违反winform中的跨线程更新规则的旧应用程序的过程中,我创build了以下扩展方法,以便在发现它们时快速修复非法呼叫: /// <summary> /// Execute a method on the control's owning thread. /// </summary> /// <param name="uiElement">The control that is being updated.</param> /// <param name="updater">The method that updates uiElement.</param> /// <param name="forceSynchronous">True to force synchronous execution of /// updater. False to allow asynchronous execution if the call is marshalled /// from a non-GUI thread. If […]
我正在尝试做一些数据转换。 不幸的是,大部分的数据是string,它应该是int或double等等。 所以我得到的是这样的: double? amount = Convert.ToDouble(strAmount); 这种方法的问题是,如果strAmount是空的,如果它是空的,我想它为null,所以当我把它添加到数据库的列将是空的。 所以我写了这个: double? amount = null; if(strAmount.Trim().Length>0) { amount = Convert.ToDouble(strAmount); } 现在,这工作正常,但我现在有五行代码,而不是一个。 这使得阅读起来更加困难,尤其是当我有大量的列转换时。 我以为我会使用string类和generics的扩展来传入types,这是因为它可能是一个双精度,或一个整数,或长。 所以我试过这个: public static class GenericExtension { public static Nullable<T> ConvertToNullable<T>(this string s, T type) where T: struct { if (s.Trim().Length > 0) { return (Nullable<T>)s; } return null; } } 但是我得到的错误:不能转换types“string”为“T?” 有没有解决的办法? 我不太熟悉使用generics创build方法。
我想在Unity3d中为Vector3类做一个扩展方法。 但我似乎不太明白。 这是我的: public static class ExtensionMethods{ public static Vector3 MaxValue(this Vector3 _vec3) { return new Vector3(float.MaxValue,float.MaxValue,float.MaxValue); } } 现在我想使用Vector3.MaxValue这个代码行来创build一个Vector3.MaxValue : Vector3 closestPoint = Vector3.MaxValue; 但是,然后我得到这个错误: error CS0117: `UnityEngine.Vector3' does not contain a definition for `MaxValue' 我知道这将工作: Vector3 closestPoint = new Vector3().MaxValue(); 但是,我创build了2个新的Vector3实例。 一个在MaxValue调用,一个在外面。 是不是可以做一个这样的代码: Vector3 closestPoint = Vector3.MaxValue;
我为ASP.NET MVC ViewPage创build了一个扩展方法,例如: public static class ViewExtensions { public static string Method<T>(this ViewPage<T> page) where T : class { return "something"; } } 从视图(从ViewPage )调用此方法时,我得到错误“ CS0103:名称'方法'不存在于当前的上下文中 ”,除非我用this关键字来调用它: <%: Method() %> <!– gives error CS0103 –> <%: this.Method() %> <!– works –> 为什么需要this关键字? 还是没有它的工作,但我错过了什么? (我认为这个问题一定有重复,但是我找不到) 更新 : 正如Ben Robinson所说 ,调用扩展方法的语法只是编译器糖。 那么为什么编译器不能自动检查当前types的基types的扩展方法,而不需要这个关键字?
我有一个预先存在的接口… public interface ISomeInterface { void SomeMethod(); } 我已经扩展了这个内置使用混合… public static class SomeInterfaceExtensions { public static void AnotherMethod(this ISomeInterface someInterface) { // Implementation here } } 我有一个这样的调用这个我想testing的类… public class Caller { private readonly ISomeInterface someInterface; public Caller(ISomeInterface someInterface) { this.someInterface = someInterface; } public void Main() { someInterface.AnotherMethod(); } } 和一个testing,我想嘲笑接口,并validation扩展方法的调用… [Test] public void Main_BasicCall_CallsAnotherMethod() […]