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); } }
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;
}
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.
source share