I found out that TypeDescriptor.AddAttributes doesn’t work in Unity. Or at least, when I try to get the new assigned type converter using TypeDescriptor.GetConverter, it doesn’t return the new converter which I assigned in the first line.
This code works perfectly in regular Visual Studio C# project and the second line returns “MyGuidConverter” as output but the same code returns “System.ComponentModel.GuidConverter” in Unity which is incorrect.
Why is that?
Is this a mono bug?
TypeDescriptor.AddAttributes(typeof(Guid), new TypeConverterAttribute(typeof(MyGuidConverter)));
Debug.Log(TypeDescriptor.GetConverter(typeof(Guid)).GetType().FullName);
Well, i saw your question already 3 days ago. However i never really used COM in C# so i can’t say much about it. However i had a look into the mscorlib of Unity’s Mono version and i can verify (to some degree) that GetConverter doesn’t look for dynamically added attributes at all. It only seems to search the actual class types.
As you said in your edit it seems to be a bug in Mono, … and that’s already the end of the story ^^.
For what purpose do you actually need COM objects? I guess that 80% of the UnityAnswers users have never heard about the component object model before and even under 99% have worked with COM objects in their life ^^. So i doubt you will get much user experience here. All people here are just developers like you and me. This isn’t something the community can “answer” or even “fix”.
Feel free to file a bug report in Unity, but chances are below 0.01% that this will be fixed any time soon ^^. Unity uses the same old Mono engine for years and this isn’t an issue that invluences the majority of the Unity users ^^.
Scripting Runtime Version = .NET 4.6:
MyGuidConverter (correct output)
using System;
using System.ComponentModel;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
void Start()
{
TypeDescriptor.AddAttributes(typeof(Guid), new TypeConverterAttribute(typeof(MyGuidConverter)));
Debug.Log(TypeDescriptor.GetConverter(typeof(Guid)).GetType().FullName);
}
}
class MyGuidConverter : TypeConverter
{
}