私はC#コンソールアプリケーションで作業しています。私の目的はGroupEntityと呼ばれるオブジェクトを作成することです。Genericクラスを非汎用クラスの中にラップC#
このGroupEntityオブジェクトは、Active Directoryのユーザーオブジェクトの属性名とそのユーザーオブジェクトの可能な値を保持する汎用型のオブジェクトを含む 'AttributeFilter'オブジェクトのリストになります。私がAttributeFilterオブジェクトを汎用型にしたい理由は、ADのユーザーオブジェクトの属性の一部がint32、int32、int64などであるためです。
ここに私のクラスがありますここにスペースを節約するため)
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
}
ここにGroupEntityクラスがあります。私には、
List<AttributeFilter<T>>
フィールドがあります。問題は
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);
}
それでは、どのように...私は
は、今私は初期化するためのProgram.csを使用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;
}
}とテストを実行するまで、そのTがどうなるか知らないです私はそれの中にAttributeFilter<T>
の様々なタイプを保持できるので、ジェネリックパラメータなしで私のGroupEntityクラスを書くことができます。たとえば、私は保持することができますAttributeFilter<int>
とAttributeFilter<string>
とAttributeFilter<long>
私はこの問題を把握していないようです。
「AttributeFilter」という非ジェネリックな「AttributeFilter」基本クラスを作成し、GroupEntityの基本クラスを参照します。 –
あなたのシステムの 'GroupEntity'オブジェクトの実際のユースケースは? 3つのフィールド( 'List>、' List > '、' List > ')を解決することができますか?そうでなければ、コンパイル時に型パラメータを知らなければ、ジェネリックはほとんどの場合、移動する方法ではありません。 –
これはほんの3つの例です。基本的に私は、ADユーザー属性で使用されるすべてのデータ型に対してAttributeFilterを作成しようとしています。 string、longおよびint32は共通です。しかし、私は文字列[]、バイト[]、DateTimeなども見ています。だから私はそれらすべてを作成する必要があります。 –
user5078178