Re[7]: массивы в коллекции
От: AlexSychev  
Дата: 07.12.05 13:51
Оценка: 2 (1)
Здравствуйте, Lexsus, Вы писали:

L>Здравствуйте, wishful, Вы писали:

W>>Посмотрите здесь. Может поможет
L>Спасибо за ссылку, буду смотреть.
Ссылка не очень информативная, наступал на теже грабли, а тема что тебе нужна уже обсуждалась на форуме.
Помоему решения я нашел имеено здесь.

вот как это у меня получилось

1) сам объект

[TypeConverter(typeof(InputFieldConverter))]
public class InputField
{
private string _name = "";
private string _label = "";

public InputField()
{

}

public InputField(string name, string label)
{
_name = name;
_label = label;
}

[DefaultValue("")]
public string Name
{
get { return _name; }
set{ _name = value; }
}

[DefaultValue("")]
public string Label
{
get { return _label; }
set{ _label = value; }
}
}


2) типизированная коллекция
public class InputFieldCollection : CollectionBase
{
public int Add(InputField inputField)
{
return InnerList.Add( inputField );
}

public InputField this[int index]
{
get { return (InputField)InnerList[index]; }
set { InnerList[index] = value; }
}

public void AddRange(InputField[] inputFields)
{
InnerList.AddRange(inputFields);
}

}

3) и обязательный TypeConverter для InputField (взято из MSDN, см InstanceDescriptor Class [C#])

internal class InputFieldConverter : TypeConverter
{
// This method overrides CanConvertTo from TypeConverter. This is called when someone
// wants to convert an instance of Triangle to another type. Here,
// only conversion to an InstanceDescriptor is supported.
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(InstanceDescriptor))
{
return true;
}

// Always call the base to see if it can perform the conversion.
return base.CanConvertTo(context, destinationType);
}

// This code performs the actual conversion from a Triangle to an InstanceDescriptor.
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(InstanceDescriptor))
{
ConstructorInfo ci = typeof(InputField).GetConstructor(new Type[]{typeof(string), typeof(string)});
InputField t = (InputField) value;
return new InstanceDescriptor(ci,new object[]{t.Name, t.Label});
}

// Always call base, even if you can't convert.
return base.ConvertTo(context, culture, value, destinationType);
}
}
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.