6

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;
}
4

1 回答 1

2

就像FromObject转换一样,您必须逐案处理。

对于NSNumbers,使用显式转换(也适用于 bool):

NSNumber number;
double d = (double)number;

对于NSStrings,隐式转换甚至可以工作:

NSString nsstring;
string converted = nsstring;

但我猜你已经计算了值类型转换。

对于引用类型,派生自的所有类型NSObject(对于您要来回传递给本机的大多数对象都是这种情况)都有一个构造函数重载,并接受一个IntPtras 参数。用那个。

NSObject native;
var label = new UILabel (native.Handle);

话虽如此,这NSObject是多余的。您的本机库可以只返回一个IntPtr( void*),您可以使用它来重新创建对象。你不必这样做:

IntPtr ptr;
NSObject nsobject = new NSObject (ptr);
于 2013-10-14T11:46:54.787 回答