Wrapping a Generic class inside a non-generic C # class

I am working on a C # console application. My goal is to create an object named GroupEntity, preferably not a generic type.

Inside this GroupEntity object, an AttributeFilter object will be created that contains a Generic object that contains the attribute name on the user object in Active Directory and the possible values ​​of these user objects. The reason I want the AttributeFilter object to be of a common type is because some attributes for user objects in AD are string attributes, some are int32, some are int64, etc.

Here are my classes (I cut out contructorse etc. to save space here)

public class AttributeFilter<T> : IEqualityComparer<AttributeFilter<T>> { private string _attributeName; private T _attributeValue; private List<T> _attributeValues { get; set; } public AttributeFilter(string attributeName) { AttributeName = attributeName; _attributeValues = new List<T>(); } public void AddValues(T attributeValue) { AttributeValue = attributeValue; if (!_attributeValues.Contains(AttributeValue)) { _attributeValues.Add(AttributeValue); } } // Ive cut out the getter setter etc that is not relevant } 

Here is the GroupEntity class. Notice that i have

 List<AttributeFilter<T>> 

field. The problem is that I don’t know what it will be for T until I ran program.cs

 public class GroupEntity<T> { private string _groupName; // because I want to a have a List<AttributeFilter<T>>, but I dont really want this here. because of program.cs when I initialise a new GroupEntity<> I have to tell it what type. I wont know. The type could be int32, string, long or whatever. private List<AttributeFilter<T>> _filters; public void AddFilters(AttributeFilter<T> attributeFilter) { if (!_filters.Contains(attributeFilter, attributeFilter)) { _filters.Add(attributeFilter); } } public GroupEntity() { _filters = new List<AttributeFilter<T>>(); } public GroupEntity(string groupName) : this() { _groupName = groupName; } 

}

Now I use program.cs to initialize and test ...

  class Program { static void Main(string[] args) { // Create AttributeFilter object for user attribute: EYAccountType var at1 = new AttributeFilter<string>("EYAccountType"); at1.AddValues("02"); at1.AddValues("03"); at1.AddValues("04"); at1.AddValues("05"); // try adding anothr AtributeFilter with same name. var at3 = new AttributeFilter<string>("EYAccountType1"); at3.AddValues("06"); at3.AddValues("07"); // Create AttributeFilter object for user attribute: userAccountControl var at2 = new AttributeFilter<int>("userAccountControl"); at2.AddValues(512); at2.AddValues(544); at2.AddValues(546); at2.AddValues(4096); // Now create a GroupEntity object var group1 = new GroupEntity<string>("My_First_AD_Group_Name"); // Try adding the above two AttributeFilter objects we created to the GroupEntity object. group1.AddFilters(at1); group1.AddFilters(at3); // This is the problem. I know why this is happening. because I initialised the var group1 = new GroupEntity<string>. So it wont accept at2 because at2 is taking in int. //group1.AddFilters(at2); } 

So, how can I write a GroupEntity class without a generic parameter so that I can store various AttributeFilter<T> types in it. So for example, I can hold AttributeFilter<int> and AttributeFilter<string> and AttributeFilter<long>

I can not understand this problem.

+5
source share
1 answer

More or less you cannot.

Generic types created by instances of different types are not related to each other (i.e. AttributeFilter<long> and AttributeFilter<int> do not get any common base class - they are like differnet like Exception and HttpClient ). Thus, it is not possible to put instances of these types in the same collection with strong typing.

The standard solution is to use a non-generic template or interface for your AttributeFilter<T> . Alternatively, save them as an object collection and lose all security of the type, or perhaps a dynamic collection, which will at least give you the opportunity to call methods (at the price of reflection).

+2
source

Source: https://habr.com/ru/post/1266437/


All Articles