I have defined an abstract method in my base class to take in a Generic type. What I want to be able to do is pass in a list of one of my sub classes to this abstract method. I am just not sure how to properly handle this in the sub class?
Base Class Abstract Method:
protected abstract void Validate<T>(T records);
SubClass Implementation (Where I am having issues):
Call Method (Pass in List of LogEventRecord):
Validate<List<LogEventRecord>>(records);
Method (Want to handle List of LogEventRecord):
protected override void Validate<T>(T records)
{
//How do I handle records here? I want them to be List<LogEventRecord> and when i debug
//They appear to be coming in that way, but I can't utilize them in the method
//How do I cast the records to List<LogEventRecord>? Is that what I do?
}
Any suggestions would be greatly appreciated.
Declare the method like this:
And call it like this:
The compiler should be able to infer the type for generic methods, no need to include it explicitly. If you really want to include the type or if type inference fails for some reason (you’ll know this because the compiler will complain at you), you can do this:
Then your implementations can use the records like this:
But right now the only thing you know about
Tis that it’s an object. You can’t do much with variables of type “object”, and so this isn’t very useful yet. In fact, the latter two options in my sample will fail right now because the.IsNotValidproperty probably doesn’t exist.Instead, you probably want (maybe already have) an interface describing the objects you will use with this function: they’re probably always going to be either logs or records of some common base type. If that’s the case, you have a two options. The first is to constrain your generic type. You do that by changing your method signature like this:
The other option is that in C# 4 there is new support for variance on interfaces (including IEnumerabe<T> that would allow you to avoid the need for a generic method here at all. To take advantage of this, just make sure you’re using .Net 4 in Visual Studio 2010 or later and declare the method like this:
You need .Net 4, and not just c# 4 targeting an earlier version of the framework, because you need the .Net 4 version of the IEnumerable<T> interface which is built to include the necessary support for variance.
Finally, there is a third option. You could add a
delegateparameter to your method to convert each item in the list to a boolean, like so:.
.