private TypeRegistry InitTypeRegistry(Type contextType, params Type[] parents) { var t = new TypeRegistry(); t.RegisterType("ViewModel", contextType); t.RegisterType("RootViewModel", parents.LastOrDefault() ?? contextType); t.RegisterType("Enumerable", typeof(Enumerable)); for (int i = parents.Length - 1; i >= 0; i--) { if (!t.ContainsKey(parents[i].Name)) t.RegisterType(parents[i].Name, parents[i]); } if (!t.ContainsKey(contextType.Name)) t.RegisterType(contextType.Name, contextType); return t; }
public void ParseAssemblyAndRegisterRosServices(Assembly assembly) { foreach (Type type in assembly.GetTypes()) { var typeInfo = type.GetTypeInfo(); if (type == typeof(RosService) || !typeInfo.IsSubclassOf(typeof(RosService))) { continue; } RosService service = Activator.CreateInstance(type) as RosService; if (service.ServiceType == "undefined/unknown") { throw new Exception("Invalid servive type. Service type field (srvtype) was not initialized correctly."); } var packageName = service.ServiceType.Split('/')[0]; if (!PackageNames.Contains(packageName)) { PackageNames.Add(packageName); } Logger.LogDebug($"Register {service.ServiceType}"); if (!TypeRegistry.ContainsKey(service.ServiceType)) { TypeRegistry.Add(service.ServiceType, service.GetType()); } } }
static Device() { TypeRegistry registry = ValueExpressionParser.DefaultTypeRegistry; // Use a proxy global settings reference, as required by Device model, // if TSL host application has not already defined one. This is // commonly only defined when host has a self-hosted web interface. if (!registry.ContainsKey("Global")) { registry.RegisterSymbol("Global", new GlobalSettings()); } }
/// <summary> /// Gets the registered types that inherit or implement the given base type. /// </summary> /// <param name="baseType">Type of which to get the children or implementors.</param> /// <returns>An IEnumerable of types that inherit or implement the given base type.</returns> /// <exception cref="ArgumentNullException">Thrown if <paramref name="baseType"/> is null</exception> public IEnumerable <TypeInfo> GetRegisteredTypes(TypeInfo baseType) { if (baseType == null) { throw new ArgumentNullException(nameof(baseType)); } if (TypeRegistry.ContainsKey(baseType)) { return(TypeRegistry[baseType]); } else { return(Enumerable.Empty <TypeInfo>()); } }
/// <summary> /// Adds the given type to the type registry. /// </summary> /// <param name="type">Type of the type register.</param> /// <remarks> /// After plugins are loaded, any type that inherits or implements the given Type can be easily found. /// If the type is already in the type registry, nothing will be done. /// </remarks> /// <exception cref="ArgumentNullException">Thrown if <paramref name="type"/> is null.</exception> public void RegisterTypeRegister(TypeInfo type) { if (type == null) { throw new ArgumentNullException(nameof(type)); } if (!TypeRegistry.ContainsKey(type)) { TypeRegistry.Add(type, new List <TypeInfo>()); // Add the type to its own registry. // The function will only add it if an instance can be created. // This will happen automatically if assembly searching is enabled, // but it needs to happen now to avoid issues where it doesn't exist until later, despite some things expecting it now. RegisterType(type, type); } }
public void ParseAssemblyAndRegisterRosMessages(Assembly assembly) { foreach (Type othertype in assembly.GetTypes()) { var messageInfo = othertype.GetTypeInfo(); if (othertype == typeof(RosMessage) || !messageInfo.IsSubclassOf(typeof(RosMessage)) || othertype == typeof(InnerActionMessage)) { continue; } var goalAttribute = messageInfo.GetCustomAttribute <ActionGoalMessageAttribute>(); var resultAttribute = messageInfo.GetCustomAttribute <ActionResultMessageAttribute>(); var feedbackAttribute = messageInfo.GetCustomAttribute <ActionFeedbackMessageAttribute>(); var ignoreAttribute = messageInfo.GetCustomAttribute <IgnoreRosMessageAttribute>(); RosMessage message; if (goalAttribute != null || resultAttribute != null || feedbackAttribute != null || ignoreAttribute != null) { Type actionType; if (goalAttribute != null) { actionType = typeof(GoalActionMessage <>); } else if (resultAttribute != null) { actionType = typeof(ResultActionMessage <>); } else if (feedbackAttribute != null) { actionType = typeof(FeedbackActionMessage <>); } else if (ignoreAttribute != null) { continue; } else { throw new InvalidOperationException($"Could create Action Message for {othertype}"); } Type[] innerType = { othertype }; var goalMessageType = actionType.MakeGenericType(innerType); message = (Activator.CreateInstance(goalMessageType)) as RosMessage; } else { message = Activator.CreateInstance(othertype) as RosMessage; if ((message != null) && (message.MessageType == "undefined/unknown")) { throw new Exception("Invalid message type. Message type field (msgtype) was not initialized correctly."); } } var packageName = message.MessageType.Split('/')[0]; if (!PackageNames.Contains(packageName)) { PackageNames.Add(packageName); } Logger.LogDebug($"Register {message.MessageType}"); if (!TypeRegistry.ContainsKey(message.MessageType)) { TypeRegistry.Add(message.MessageType, message.GetType()); } else { var messageFromRegistry = CreateMessage(message.MessageType); if (messageFromRegistry.MD5Sum() != message.MD5Sum()) { throw new InvalidOperationException($"The message of type {message.MessageType} has already been " + $"registered and the MD5 sums do not match. Already registered: {messageFromRegistry.MD5Sum()} " + $"new message: {message.MD5Sum()}."); } else { Logger.LogDebug($"The message of type {message.MessageType} has already been registered. Since the" + "MD5 sums do match, the new message is ignored."); } } } }