I have some code I'm working on that is stumping me. I'm having trouble putting the right words into Google to find the correct answer.
I need to test some code (which I cannot change) by calling the code's high-level entry point here:
public static T FindOne<T>(string propertyName, object propertyValue) where T : Entity
{
return (T)Entity.FindOne(typeof(T), propertyName, propertyValue);
}
In this case, "T" can be a fairly large variety of sub-classes of the "Entity" type. Each sub class is interesting in its own way and has all sorts of different kinds of members to the class. I already wrote some separate independent tests which each called that entry point separately. Like this:
Test 1:
var objectToTest = Entity.FindOne<TheFirstOfThoseInterestingSubClasses> ("Id", this.testData.Id.ToString());
Test 2:
var objectToTest = Entity.FindOne<TheSecondOfThoseInterestingSubClasses> ("Id", this.testData.Id.ToString());
Test 3:
var objectToTest = Entity.FindOne<TheThirdOfThoseInterestingSubClasses> ("Id", this.testData.Id.ToString());
( . . . ) (etc)
Each time, "objectToTest" is returned as another one of those interesting sub-classes, which I can then test and check.
My code reviewer rightfully suggested that, instead, my test should be refactored as a single parent test class with child classes that invoked the tests on the various objects. This would mean less code duplication and higher maintainability.
However, in this new refactored arrangement, I can't put the name of the interesting sub class directly into the angle brackets, since, I don't know what it's going to be each time round. It has to be determined at run time by querying my test input object. What I
want to do is something like this:
var objectToTest = Entity.FindOne<MyTestInputObject.GetType()> ("Id", this.testData.Id.ToString());
Which doesn't work, and results in the cryptic error "Operator '<' cannot be applied to operands of type 'method group' and 'Type'". The error, of course, has nothing to do with what's really wrong, which is, I can't put a variable inside those brackets since the type has to be determined at compile time rather than runtime.
What is my workaround here? I've tried googling, but I can't seem to get the search terms correct. I keep coming up with hits that almost get close to what I want to do, but not quite, not enough to work the way I need them to work. I've actually tried some of them and none of the examples have anything that looks the same as what I'm trying to do. For example there's one with a Method.Invoke that looks promising, but I can't get any of the parameters correct to make it work.
Anyone happen to know this one?