private static Matcher<T> EqualTo<T>(T item) { return new IsEqual<T>(item); }
How to change the above definition of a method so that the following are valid / valid.
EqualTo("abc"); EqualTo(4); EqualTo(null);
Trying to port some Java code where null
seems to be an acceptable value for the T
parameter.
Update
Thanks: for all the answers - especially Imon and Jason. I did not want the method call to worry about type-output. The next overload fixed him.
private static Matcher<object> EqualTo(object item) { return EqualTo<object>(item); }
Actually, the above question was part of a bigger mystery. The ultimate goal was as follows.
this.AssertThat(null, EqualTo(null)); this.AssertThat(null, Not(EqualTo("hi"))); this.AssertThat("hi", Not(EqualTo(null)));
The same fix applies .. RFC . (Ignoring part of the ugly extension method - this is another problem. I wanted to have these methods in all test records without inheritance.)
public static void AssertThat<T>(this object testFixture, object actual, Matcher<T> matcher, string message = "") { AssertThat(anyObject, (T)actual, matcher, message); } public static void AssertThat<T, TSuper>(this object testFixture, T actual, Matcher<TSuper> matcher, string message = "") where T : TSuper { ... check and assert
source share