public ManagedObject (object obj)
{
// Console.WriteLine ("new ManagedObject created wrapping object of type {0}, handle == {1}", obj.GetType(), Handle);
managed = obj;
Type type = obj.GetType ();
bool isScriptable = type.IsDefined (typeof(ScriptableTypeAttribute), true);
// add properties
foreach (PropertyInfo pi in type.GetProperties ()) {
if (!isScriptable && !pi.IsDefined (typeof(ScriptableMemberAttribute), true))
continue;
RegisterScriptableProperty (pi);
if (RegisterScriptableTypes (pi))
HasTypes = true;
}
// add events
foreach (EventInfo ei in type.GetEvents ()) {
if (!isScriptable && !ei.IsDefined (typeof(ScriptableMemberAttribute), true))
continue;
RegisterScriptableEvent (ei);
HasEvents = true;
// XXX toshok - do we need to RegisterScriptableTypes for parameters on the delegate?
}
// add functions
foreach (MethodInfo mi in type.GetMethods ()) {
if (!isScriptable && !mi.IsDefined (typeof(ScriptableMemberAttribute), true))
continue;
RegisterScriptableMethod (mi);
if (RegisterScriptableTypes (mi))
HasTypes = true;
}
if (HasTypes) {
TypeOps typeOps = new TypeOps ();
Type typeOpsType = typeof (TypeOps);
RegisterBuiltinScriptableMethod (typeOpsType.GetMethod ("CreateManagedObject"), "createManagedObject", typeOps);
}
if (HasEvents) {
EventOps eventOps = new EventOps (this);
Type eventOpsType = typeof (EventOps);
RegisterBuiltinScriptableMethod (eventOpsType.GetMethod ("AddEventListener"), "addEventListener", eventOps);
RegisterBuiltinScriptableMethod (eventOpsType.GetMethod ("RemoveEventListener"), "removeEventListener", eventOps);
}
RegisterScriptableMethod (type.GetMethod ("ToString"), "toString");
if (ManagedObject is IList) {
if (type.GetProperty ("Length") != null)
RegisterScriptableProperty (type.GetProperty ("Length"), "length");
else if (type.GetProperty ("Count") != null)
RegisterScriptableProperty (type.GetProperty ("Count"), "length");
foreach (MethodInfo mi in type.GetMethods ()) {
switch (mi.Name) {
case "IndexOf":
RegisterScriptableMethod (mi, "indexOf");
break;
case "LastIndexOf":
RegisterScriptableMethod (mi, "lastIndexOf");
break;
case "ToArray":
RegisterScriptableMethod (mi, "toArray");
break;
}
}
ListOps listOps = new ListOps ((IList)ManagedObject);
Type listOpsType = typeof (ListOps);
if (type.GetProperty ("Item") != null)
RegisterScriptableProperty (type.GetProperty ("Item"), "item");
else
RegisterScriptableProperty (listOpsType.GetProperty ("Item"), "item", listOps);
RegisterScriptableMethod (listOpsType.GetMethod ("Pop"), "pop", listOps);
RegisterScriptableMethod (listOpsType.GetMethod ("Push"), "push", listOps);
RegisterScriptableMethod (listOpsType.GetMethod ("Reverse"), "reverse", listOps);
RegisterScriptableMethod (listOpsType.GetMethod ("Shift"), "shift", listOps);
RegisterScriptableMethod (listOpsType.GetMethod ("Unshift"), "unshift", listOps);
RegisterScriptableMethod (listOpsType.GetMethod ("Splice"), "splice", listOps);
} else if (ManagedObject is IDictionary) {
DictOps dictOps = new DictOps ((IDictionary)ManagedObject);
Type dictOpsType = typeof (DictOps);
RegisterScriptableProperty (dictOpsType.GetProperty ("Item"), "item", dictOps);
}
}