1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- using Island.StandardLib.Reflection.Exception;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- namespace Island.StandardLib.Reflection
- {
- public class DynamicClassInstance : DynamicExtendableInstance
- {
- public DynamicManager Manager { get; private set; }
- public DynamicType ClassType { get; private set; }
- public object Instance { get; private set; }
- public DynamicClassInstance(DynamicManager manager, DynamicType type, params object[] initargs)
- {
- Manager = manager;
- ClassType = type;
- if (initargs == null) initargs = new object[1] { null };
- initargs = initargs.Do(r => r is DynamicClassInstance instance ? instance.Instance : r);
- Instance = Activator.CreateInstance(type.ClassType, initargs);
- }
- public DynamicClassInstance(DynamicManager manager, object instance)
- {
- Manager = manager;
- ClassType = Manager.GetDynamicType(instance.GetType());
- Instance = instance;
- }
- public object Call(string methodName, params object[] args)
- {
- if (args == null) args = new object[1] { null };
- args = args.Do(r => r is DynamicClassInstance instance ? instance.Instance : r);
- foreach (var t in ClassType.FindMethod(methodName))
- {
- try
- {
- return t.Invoke(Instance, args.Length == 0 ? null : args);
- }
- catch (TargetParameterCountException)
- {
- continue;
- }
- }
- throw new MethodNotFoundException(ClassType.ClassType.Name, methodName);
- }
- public DynamicClassInstance CallR(string methodName, params object[] args)
- {
- return new DynamicClassInstance(Manager, Call(methodName, args));
- }
- public object this[string propertieName]
- {
- get => GetPropertie(propertieName);
- set => SetPropertie(propertieName, value);
- }
- public DynamicClassInstance this[string propertieName, object flag]
- {
- get => new DynamicClassInstance(Manager, GetPropertie(propertieName));
- set => SetPropertie(propertieName, value.Instance);
- }
- public void SetPropertie(string propertie, object value) => Call("set_" + propertie, value);
- public object GetPropertie(string propertie) => Call("get_" + propertie);
- public T As<T>() where T : class => Manager as T;
- }
- public class DynamicClassInstance<T> : DynamicClassInstance where T : class
- {
- public DynamicClassInstance(DynamicManager manager, DynamicType type, params object[] initargs)
- : base(manager, type, initargs) { }
- public static implicit operator T(DynamicClassInstance<T> instance) => instance.As<T>();
- }
- }
|