[TypeConverter(typeof(IntBoolString.IntBoolStringConverter))]
public class IntBoolString {
private int intVal;
private string stringVal;
private bool boolVal;
public IntBoolString(string s, int I, bool b) {
This.intVal = I;
This.stringVal =s ;
This.boolVal = b;
}
public bool Bool{
get {return boolVal;}
set {boolVal = value;}
}
public int Int {
get {return intVal;}
set {intVal = value;}
}
public string String {
get {return stringVal;}
set {stringVal = value;}
}
if (value is string) {
string stringValue = (string)value;
int intValue;
bool boolValue;
int commaIndex =
stringValue.IndexOf(',');
if (commaIndex != -1) {
intValue = Int32.
Parse(stringValue.
Substring(0, commaIndex));
commaIndex = stringValue.
IndexOf(',',
commaIndex + 1);
if (commaIndex != -1) {
int nextComma = stringValue.IndexOf(',', commaIndex + 1);
if (nextComma != -1) {
boolValue = Boolean.Parse(stringValue.Substring(commaIndex+1,
nextComma - commaIndex));
stringValue = stringValue.Substring(nextComma+1);
return new IntBoolString(intVal, boolVal, stringValue);
}
}
}
throw new FormatException("Can't convert '" + stringValue + "' to IntBoolString Object");
}
}
public override PersistInfo GetPersistInfo(ITypeDescriptorContext context, object value) {
if (value is IntBoolString) {
IntBoolString ibs = (IntBoolString)value;
return new CreationBundle(typeof(IntBoolString), null,
new CreationArgument[] {
new CreationArgument(ibs.Int, typeof(Int32)),
new CreationArgument(ibs.Bool, typeof(bool)),
new CreationArgument(ibs.String, typeof(string))});
}
return base.GetPersistInfo(context, value);
}
}
public override object CreateInstance(ITypeDescriptorContext
context, IDictionary propertyValues) {
return new IntBoolString((int)propertyValues["Int"],
(bool)propertyValues["Bool"],
(string)propertyValue["String"]);
}
public override bool GetCreateInstanceSupported(ITypeDescriptorContext context) {
return true;
}
反之,.Net Framework 在這種情況下,則使用實行 ICollection 物件的集合。物件可建立出集合並傳送給其它物件,同時參照項目可視基本物件的變更而保持最新狀態。若另一物件也針對集合進行變更,則亦將同時通知該物件。對於使用集合的 .Net Framework 設計工具,還需要支援含有 Get 和 Set 的 All 屬性,且其類型必須為集合可保留的物件陣列。例如:
public class IntCollection : ICollection {
private int[] values;
public IntCollection(int[] intValues) {
this.values = (int[])intValues.Clone();
}
public int[] All {
get {
return (int[])values.Clone();
}
set {
values = (int[])value.Clone();
}
}
public int Count {
get {
if (values == null) {
return 0;
}
return values.Length;
}
}
[Browsable(false)]
public object SyncRoot {
get {
return this;
}
}
[Browsable(false)]
public bool IsReadOnly {
get {
return false;
}
}
[Browsable(false)]
public bool IsSynchronized {
get {
return true;
}
}
}
public class RegistryLicenseProvider: LicenseProvider {
public override License GetLicense(
LicenseContext context,
Type type,
object instance,
bool allowExceptions) {