Help with generics

  • Thread starter Thread starter GiJeet
  • Start date Start date
G

GiJeet

Someone please help me. I am trying to create a generic validation
method that simply checks to ensure the type has a value.

Here is the method that calls the validation method:

public void CheckParameterTest(string someString,
List<SqlParameter> sqlParams, List<Customers> customers) {

ValidateParams(sqlParams);

}

Here is the generic validation method.
I just want to check that the List<whatever>.Count property is not
zero.
I can't seem to get the count of the list. Please help

private void ValidateParams<T>(T param) {
Type type = param.GetType();
if (type.IsGenericType && type.GetGenericTypeDefinition() ==
typeof(List<>)) {
Type[] typeParameters = type.GetGenericArguments();
//typeParameters.Length always returns 1 even if the list.Count is
zero!

// I can get the type in the list but still can't get the count of
the list!
Type itemType = type.GetGenericArguments()[0];

}


//string check will go here

}

Thanks.
 
GiJeet said:
Someone please help me. I am trying to create a generic validation
method that simply checks to ensure the type has a value.

Here is the method that calls the validation method:

public void CheckParameterTest(string someString,
List<SqlParameter> sqlParams, List<Customers> customers) {

ValidateParams(sqlParams);

}

Why do you want the ValidateParams() method to be generic? Are you
using it with things other than List<T>? Or will the argument always be
List<T>? If the latter, then this should suffice:

void ValidateParams<T>(List<T> param)
{
if (param.Count == 0)
{
// report error somehow
}
}

If you need to validate a variety of argument types, not just List<T>:

void ValidateParams<T>(T param)
{
ICollection collection = param as ICollection;

if (collection != null && collection.Count == 0)
{
// report error somehow
}
}

But then, it's not clear why the method should be generic at all:

void ValidateParams(object param)
{
ICollection collection = param as ICollection;

if (collection != null && collection.Count == 0)
{
// report error somehow
}
}

Or even:

void ValidateParams(ICollection param)
{
if (param.Count == 0)
{
// report error somehow
}
}

Basically, there are LOTS of ways to do this, none of them requiring
reflection. Without a more complete problem statement from you, it's
not clear why you want a generic method in the first place. But
hopefully the above gives you enough ideas to make progress.

By the way, FYI: your comments in the code you posted seem to imply that
you think that the GetGenericArguments() method is supposed to for some
reason return an array that has the same length as the length of the
collection passed as the method arguments. This could not be further
from the truth. The code you posted is doing nothing at all with the
instance passed to the method. Instead, it's inspecting the generic
type _of_ that argument. And the GetGenericArguments() method is
returning the list of generic type arguments used when declaring the
_type_ of the argument (i.e. in this case, it's a single Type, and that
type is the fully-qualified "SqlParameter" type).

Hope that helps.

Pete
 
Why not create a

IValidate
{
bool Validate{}
}

in each model class inherit IValidate and do the validation code.

In a validation helper just pass in the model as an IValidate and cal the
Validate method.

Job done with more control....
 
Back
Top