private void OnObjectContainerGetted(IObjectContainerContext ctx) { if (ctx.Container != null) { return; } var objectKey = ctx.ObjectKey; if (!(objectKey is Type)) { return; } var objectType = (Type)objectKey; if (!objectType.IsDefined(typeof(RemotingServiceAttribute), true)) { return; } var attribute = objectType.GetCustomAttribute <RemotingServiceAttribute>(); //自动生成 IObjectContainer container = new SingletonObjectContainer(); container.Init(new ObjectDescription(objectType, new Hashtable { { typeof(RemotingServiceAttribute), attribute } }), this.ObjectBuilder); ctx.Namespace.AddObject(objectType, container); ctx.Container = container; }
private static IObjectContainer AddSingletonObjectToObjectNamespace(string name, Type type, object instance, IObjectNamespace objectNamespace) { IObjectContainer container = new SingletonObjectContainer(); container.Init(null, null); container.SetObject(instance, null); if (!string.IsNullOrEmpty(name)) { objectNamespace.AddObject(name, container); } if (type != null) { objectNamespace.AddObject(type, container); } return(container); }
public void HandleAttributes(IObjectServiceContext ctx, IList <CoreAttribute> attributes) { if (attributes == null || attributes.Count <= 0) { return; } var builder = new SettingObjectBuilder(ctx.ObjectService); foreach (SettingObjectAttribute attribute in attributes) { var items = new Hashtable { { typeof(SettingObjectAttribute), attribute } }; var type = attribute.OwnerType; IObjectContainer container = new SingletonObjectContainer(); container.Init(new ObjectDescription(type, items), builder); var objects = ctx.GlobalObjectNamespace; objects.AddObject(type, container); if (type.IsClass) { //查找此类型实现的接口 var interfaces = type.GetInterfaces(); if (interfaces.Length > 0) { foreach (var ifType in interfaces) { if (ifType.IsDefined(typeof(ServiceAttribute), true)) { objects.AddObject(ifType, container); } } } } } }