Continued From : http://social.msdn.microsoft.com/Forums/vstudio/en-US/ea308f43-7487-4575-99bc-ac30a0278f77/how-does-typedescriptor-work
Now I wanna ask you deeply about this problem:
1) If I add the Attribute here, you can see it will support converting:
namespace CSharp
{
[TypeConverter(typeof(MyConverter))]
class My
{
}
// You should add the below line
class MyConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string);
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(TypeDescriptor.GetConverter(typeof(My)).CanConvertFrom(typeof(string)));
Console.Read();
}
}
}
Now struct types such int (Int32...), I don't see anything like this attribute on them, but it also supports Converting……Why?
/// <summary>Represents a 32-bit signed integer.</summary>
/// <filterpriority>1</filterpriority>
[__DynamicallyInvokable]
[ComVisible(true)]
[Serializable]
public static TypeConverter GetConverter(object component, bool noCustomTypeDesc)
{
return TypeDescriptor.GetDescriptor(component, noCustomTypeDesc).GetConverter();
}
And continue:
internal static ICustomTypeDescriptor GetDescriptor(object component, bool noCustomTypeDesc)
{
if (component == null)
{
throw new ArgumentException("component");
}
if (component is TypeDescriptor.IUnimplemented)
{
object[] fullName = new object[] { component.GetType().FullName };
throw new NotSupportedException(SR.GetString("TypeDescriptorUnsupportedRemoteObject", fullName));
}
ICustomTypeDescriptor typeDescriptor = TypeDescriptor.NodeFor(component).GetTypeDescriptor(component);
ICustomTypeDescriptor customTypeDescriptor = component as ICustomTypeDescriptor;
if (!noCustomTypeDesc && customTypeDescriptor != null)
{
typeDescriptor = new TypeDescriptor.MergedTypeDescriptor(customTypeDescriptor, typeDescriptor);
}
return typeDescriptor;
}
Notice the above codes, it tells me that if I implement the interface of ICustomTypeDescriptor to my and return a new instance of TypeConverter, it should work……
namespace CSharp
{
class My:ICustomTypeDescriptor
{
public AttributeCollection GetAttributes()
{
throw new NotImplementedException();
}
public string GetClassName()
{
throw new NotImplementedException();
}
public string GetComponentName()
{
throw new NotImplementedException();
}
public TypeConverter GetConverter()
{
return new MyConverter();
}
public EventDescriptor GetDefaultEvent()
{
throw new NotImplementedException();
}
public PropertyDescriptor GetDefaultProperty()
{
throw new NotImplementedException();
}
public object GetEditor(Type editorBaseType)
{
throw new NotImplementedException();
}
public EventDescriptorCollection GetEvents(Attribute[] attributes)
{
throw new NotImplementedException();
}
public EventDescriptorCollection GetEvents()
{
throw new NotImplementedException();
}
public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
throw new NotImplementedException();
}
public PropertyDescriptorCollection GetProperties()
{
throw new NotImplementedException();
}
public object GetPropertyOwner(PropertyDescriptor pd)
{
throw new NotImplementedException();
}
}
// You should add the below line
class MyConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string);
}
}
class Program
{
static void Main(string[] args)
{
// Because your MyConverter inherits from TypeConverter
Console.WriteLine(TypeDescriptor.GetConverter(typeof(My),false).CanConvertFrom(typeof(string)));
Console.Read();
}
}
}
It REALLY gives me True!
Now here comes several questions:
1) What's this below? What will it return?
ICustomTypeDescriptor typeDescriptor = TypeDescriptor . NodeFor ( component ). GetTypeDescriptor ( component );
2) Int32 or some struct classes don't have [Typeof……], but they also support TypeConverter……Why?
Many thanks!
If you think one reply solves your problem, please mark it as An Answer , if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report
No comments:
Post a Comment