Reputation: 6866
I am currently working a web form application where I am using Devexpress
controls. The controls I am using are (but not limited to):
The form is fairly big so what I am trying to achieve is pass in e.g. IEnumerable<T>
and then loop over the collection and set the ReadOnly
property to true
. I know I can do one control at a time but I have over 50 controls so I was wondering if there is a more generic way.
I have the following snippet:
public static void MakeControlReadOnly<T>(this IEnumerable<T> controlCollection)
{
foreach(var c in controlCollection)
c.ReadOnly = true;
}
But I keep getting an error:
Severity Code Description Project File Line Suppression State Error CS1061 'T' does not contain a definition for 'ReadOnly' and no extension method 'ReadOnly' accepting a first argument of type 'T' could be found (are you missing a using directive or an assembly reference?)
I understand the error but I am not sure how to overcome it.
Upvotes: 0
Views: 416
Reputation: 7204
Try implicit type T
as the ASPxEditBase
, the base editable control of DevExpress
public static void MakeControlReadOnly<T>(this IEnumerable<T> controlCollection)
where T: ASPxEditBase
{
controlCollection.ToList().ForEach(x=> x.ReadOnly = true);
}
Upvotes: 3
Reputation: 4341
You can put a type constraint on the generic for the base class that contains the readonly property
public static void MakeControlReadOnly<T>(this IEnumerable<T> controlCollection) where T: BaseType //Set this to the correct base type
{
foreach(var c in controlCollection)
c.ReadOnly = true;
}
Upvotes: 2