NSObject.FromObject 的反面是什么,例如,从 NSObject 中获取常规 C# 对象。可以理解,这仅适用于简单类型。
更新。假设我什至知道类型 - 但不是在运行时之前。我试过了
Convert.ChangeType (value, info.PropertyType, null)
但我收到一条消息“值不是可转换对象:MonoMac.Foundation.NSString 到 System.String”。
更新 2. FromObject 的工作原理:
using MonoMac.ObjCRuntime;
using System;
using System.Drawing;
public static NSObject FromObject (object obj)
{
NSObject result;
if (obj == null)
{
result = NSNull.Null;
}
else
{
Type type = obj.GetType ();
if (type == typeof(NSObject) || type.IsSubclassOf (typeof(NSObject)))
{
result = (NSObject)obj;
}
else
{
switch (Type.GetTypeCode (type))
{
case 3:
result = new NSNumber ((bool)obj);
return result;
case 4:
result = new NSNumber ((ushort)((char)obj));
return result;
case 5:
result = new NSNumber ((sbyte)obj);
return result;
case 6:
result = new NSNumber ((byte)obj);
return result;
case 7:
result = new NSNumber ((short)obj);
return result;
case 8:
result = new NSNumber ((ushort)obj);
return result;
case 9:
result = new NSNumber ((int)obj);
return result;
case 10:
result = new NSNumber ((uint)obj);
return result;
case 11:
result = new NSNumber ((long)obj);
return result;
case 12:
result = new NSNumber ((ulong)obj);
return result;
case 13:
result = new NSNumber ((float)obj);
return result;
case 14:
result = new NSNumber ((double)obj);
return result;
case 18:
result = new NSString ((string)obj);
return result;
}
if (type == typeof(IntPtr))
{
result = NSValue.ValueFromPointer ((IntPtr)obj);
}
else
{
if (type == typeof(SizeF))
{
result = NSValue.FromSizeF ((SizeF)obj);
}
else
{
if (type == typeof(RectangleF))
{
result = NSValue.FromRectangleF ((RectangleF)obj);
}
else
{
if (type == typeof(PointF))
{
result = NSValue.FromPointF ((PointF)obj);
}
else
{
INativeObject nativeObject = obj as INativeObject;
if (nativeObject != null)
{
result = Runtime.GetNSObject (nativeObject.Handle);
}
else
{
result = null;
}
}
}
}
}
}
}
return result;
}