/// <summary> /// Gets the <see cref="RuntimeType"/> with the specified identifier. /// </summary> /// <value>The <see cref="RuntimeType"/>.</value> /// <param name="typeFullName">The identifier.</param> /// <param name="type">The type.</param> /// <returns></returns> IRuntimeType this[string typeFullName, IRuntimeType type] { get { if (!Types.ContainsKey(typeFullName)) { Types.Add(typeFullName, type); } Types[typeFullName].RegisterTypeDefinition(type.TypeFullName); return(Types[typeFullName]); } }
private object GetInstance(string[] args, IRuntimeType runtimeType) { if (runtimeType.GetInstance) { if (UseValueTypes) { return(runtimeType.CreateValueInstance()); } return(runtimeType.CreateInstance(args)); } return(runtimeType.CreateInstance(GetRuntimeTypeParameters(runtimeType, args))); }
private void hookupDisconnectedEvent(IRuntimeType rt) { if (rt != null && !m_hookedUpDisconnectedEvent) { var d = Dispatcher.CurrentDispatcher; // capture the dispatcher outside the callback rt.Disconnected += new EventHandler((s, e) => { d.BeginInvoke((Action)(() => Disconnect())); }); m_hookedUpDisconnectedEvent = true; } }
IEnumerable <IEnumerable <string> > GetRuntimeTypeInvariants(IRuntimeType runtimeType, IRuntimeType head, IEnumerable <IRuntimeType> tail) { foreach (var h in head.Types) { if (tail.Any()) { foreach (var t in GetRuntimeTypeInvariants(runtimeType, tail.First(), tail.Skip(1))) { yield return new List <string> { h } } }
static Type GetType(IRuntimeType runtime_type) { Type type = RuntimeType.GetSystemType(runtime_type); if (type == null) { throw new TypeLoadException( string.Format(Resources.Resources.TypeLoad_CreateInstance, runtime_type.Type) + Resources.Resources.TypeLoad_TypeNotFound); } return(type); }
private object GetInstance(object[] args, IRuntimeType runtimeType) { if (runtimeType.GetInstance) { if (UseValueTypes) { return(runtimeType.CreateValueInstance()); } return(runtimeType.CreateInstance()); } //Types[typeFullName].CreateInstance(args) return(runtimeType.CreateInstance(GetRuntimeTypeParameters(runtimeType, args))); }
/// <summary> /// Gets the <see cref="System.Type"/> of a class defined by /// <paramref name="runtime_type"/>. /// </summary> /// <param name="runtime_type"> /// A <see cref="IRuntimeType"/> object containing information about the /// type to be loaded. /// </param> /// <returns> /// The type instance that represents the exact runtime type of the /// specified provider or null if the type could not be loaded. /// </returns> /// <exception cref="ArgumentNullException"> /// <paramref name="runtime_type"/> is <c>null</c>. /// </exception> /// <exception cref="FileNotFoundException"> /// Tje assembly referenced by the <paramref name="runtime_type"/> is not /// found. /// </exception> /// <exception cref="PathTooLongException"> /// The path for the assembly referenced by the /// <paramref name="runtime_type"/> is too long. /// </exception> /// <remarks> /// We try to load the type's assembly using this location defined by the /// <see cref="IRuntimeType.Location"/> property of /// <paramref name="runtime_type"/> and them load the type from that /// assembly. /// </remarks> public static Type GetSystemType(IRuntimeType runtime_type) { if (runtime_type == null) { throw new ArgumentNullException("runtime_type"); } // Try to get the type from the loaded assemblies. Type type = System.Type.GetType(runtime_type.Type); // attempt to load .NET type of the provider. If the location of the // assembly is specified we need to load the assembly and try to get the // type from the loaded assembly. The name of the assembly will be // extracted from the provider type. if (type == null) { string assembly_name = runtime_type.Type; int num = assembly_name.IndexOf(','); if (num == -1) { // try to load the type from the calling assembly if a explicit // assembly was not specified. return(Assembly.GetCallingAssembly().GetType(runtime_type.Type)); } assembly_name = assembly_name.Substring(num + 1).Trim(); int num2 = assembly_name.IndexOfAny(new char[] { ' ', ',' }); if (num2 != -1) { assembly_name = assembly_name.Substring(0, num2); } string assembly_path = Path.Combine(runtime_type.Location, assembly_name); // If the file does not exists append one of the know extensions // to it and try again. if (!File.Exists(assembly_path)) { assembly_path = Path.Combine(runtime_type.Location, assembly_name) + ".dll"; if (!File.Exists(assembly_path)) { assembly_path = Path.Combine(runtime_type.Location, assembly_name) + ".exe"; } } Assembly assembly = Assembly.LoadFrom(assembly_path); type = assembly.GetType(runtime_type.Type.Substring(0, num)); } return(type); }
/// <summary> /// Creates the instance. /// </summary> /// <returns></returns> public object CreateInstance(IRuntimeType instance) { var obj = Activator.CreateInstance(instance.ActivatorType); var runtimeTypes = instance.RuntimeTypes.ToArray(); for (int i = 0; i < runtimeTypes.Length; i++) { var runtimeType = runtimeTypes[i]; var value = runtimeType.GetValue(instance.Attribute, instance.Id); var propertyInfo = PropertyCache.GetPropertyInfo(runtimeType); propertyInfo.SetValue(obj, value); } return(obj); }
/// <summary> /// Creates the instance with type inferenced evaluated arguments. /// </summary> /// <param name="type">The type.</param> /// <param name="attribute">The attribute.</param> /// <returns></returns> public object CreateInstance(IRuntimeType instance, IRuntimeType type, IRuntimeAttribute attribute) { var obj = instance.GetValue(type.Attribute, type.Id) ?? Activator.CreateInstance(instance.ActivatorType); var runtimeTypes = instance.RuntimeTypes.ToArray(); for (int i = 0; i < runtimeTypes.Length; i++) { var p = runtimeTypes[i]; var value = p.Evaluate(type, attribute, i); var id = Format.GetActivatorType(p); var propertyInfo = (PropertyInfo)instance.GetValue(id); propertyInfo.SetValue(obj, value); } return(obj); }
internal static T CreateInstance(IRuntimeType runtime_type, bool fallback, bool throw_exceptions, params object[] args) { Type type = RuntimeType.GetSystemType(runtime_type); if (type == null) { if (!throw_exceptions) { return(default(T)); } throw new TypeLoadException(GetTypeNotFoundMessage(runtime_type.Type)); } return(CreateInstance(type, fallback, throw_exceptions, args)); }
private Guid firstRuntimeId() { Guid defaultTypeId; IRuntimeType firstRuntime = null; foreach (var rt in TypeList) { firstRuntime = rt; break; } if (firstRuntime != null) { defaultTypeId = firstRuntime.TypeId; } else { defaultTypeId = Guid.NewGuid(); InvalidProperty(m_TypeIdName); } return(defaultTypeId); }
public ArduinoRuntimeProxy( IRuntimeType runtimeType, string comPort, Lazy <IRuntimeService> runtimeService, IMessagingService messagingService) { if (runtimeType == null) { throw new ArgumentNullException("runtimeType"); } if (runtimeService == null) { throw new ArgumentNullException("runtimeService"); } if (messagingService == null) { throw new ArgumentNullException("messagingService"); } this.Type = runtimeType; this.m_runtimeService = runtimeService; this.m_arduinoRuntimeProtocol = new ArduinoRuntimeProtocol(comPort); this.messagingService = messagingService; this.m_runtimeService.Value.SignalChanged += (s) => { Console.WriteLine("Signal {0} changed. Forced: {1}, Forced Value: {2}", s.SignalName.ToString(), s.Forced.BoolValue, s.ForcedValue.Value.ToString()); }; this.m_arduinoRuntimeProtocol.Connect(); this.m_information = this.m_arduinoRuntimeProtocol.GetInformation(); this.m_arduinoRuntimeProtocol.Disconnect(); this.m_signalTable = new SignalTable(this.m_information); this.m_compiler = new ArduinoLadderCompiler(this.m_information); }
/// <summary> /// Converts runtime type to corresponding parameter string value /// </summary> /// <param name="runtimeType">Runtime type</param> /// <returns>Returns runtime type parameter</returns> public static string GetActivatorType(IRuntimeType runtimeType) => string.Format(":{0}", runtimeType.ActivatorType);
/// <summary> /// Evaluates arguments /// </summary> /// <param name="runtimeTypes">Runtime types</param> /// <param name="runtimeType">Runtime type</param> /// <param name="runtimeAttribute">Runtime attribute</param> /// <returns>Returns runtime types evalated values</returns> public static object[] Evaluate(this IEnumerable <IRuntimeType> runtimeTypes, IRuntimeType runtimeType, IRuntimeAttribute runtimeAttribute) => runtimeTypes.Select((p, i) => p.Evaluate(runtimeType, runtimeAttribute, i)).ToArray();
internal void Add(IRuntimeType runtimeType) { _runtimeTypes.Add(new Tuple <Type, IRuntimeType>(runtimeType.GetType(), runtimeType)); }
/// <summary> /// Creates a new instance of the type defined by the /// <paramref name="runtime_type"/> object. /// </summary> /// <param name="runtime_type"> /// A <see cref="IRuntimeType"/> object containing information about /// the type to be instantiated. /// </param> /// <param name="args"> /// An array of arguments that match the parameters of the constructor to /// invoke. If args is an empty array or null, the constructor that takes /// no parameters(the default constructor) is invoked. /// </param> /// <returns> /// An instance of the type defined by the <paramref name="runtime_type"/> /// object casted to <typeparamref name="T"/> or <c>null</c> if the class /// could not be loaded or casted to <typeparamref name="T"/> /// </returns> /// <remarks> /// A exception is never raised by this method. If a exception is raised /// by the object constructor it will be catched and <c>null</c> will be /// returned. If you need to know about the exception, use the method /// <see cref="CreateInstance"/> instead. /// </remarks> /// <seealso cref="CreateInstance(Nohros.IRuntimeType,object[])"/> /// <seealso cref="IRuntimeType"/> public static T CreateInstanceNoException(IRuntimeType runtime_type, params object[] args) { return(CreateInstance(runtime_type, false, false, args)); }
/// <summary> /// Creates the instance. /// </summary> /// <returns></returns> public object CreateValueInstance(IRuntimeType instance) => Activator.CreateInstance(instance.ActivatorType);
/// <summary> /// Creates a new instance of the type defined by the /// <paramref name="runtime_type"/> object and the specified /// arguments, falling back to the default class constructor. /// </summary> /// <param name="runtime_type"> /// A <see cref="IRuntimeType"/> object containing information about /// the type to be instantiated. /// </param> /// <param name="args"> /// An array of arguments that match the parameters of the constructor to /// invoke. If args is an empty array or null, the constructor that takes /// no parameters(the default constructor) is invoked. /// </param> /// <returns> /// An instance of the type defined by the <paramref name="runtime_type"/> /// object casted to <typeparamref name="T"/>. /// </returns> /// <remarks> /// If a constructor that match the specified array of arguments is not /// found, this method try to create an instance of the type defined by /// the <paramref name="runtime_type"/> object using the constructor that /// takes no parameters(the default constructor). /// </remarks> /// <seealso cref="CreateInstance(Nohros.IRuntimeType,object[])"/> /// <seealso cref="IRuntimeType"/> public static T CreateInstanceFallback(IRuntimeType runtime_type, params object[] args) { return(CreateInstance(runtime_type, true, true, args)); }
/// <summary> /// Sets /// </summary> /// <param name="runtimeType"></param> public void SetRuntimeType(IRuntimeType runtimeType) => RuntimeType = runtimeType;
/// <summary> /// Creates the instance with type inferenced evaluated arguments. /// </summary> /// <param name="type">The type.</param> /// <param name="attribute">The attribute.</param> /// <returns></returns> public object CreateInstance(IRuntimeType instance, IRuntimeType type, IRuntimeAttribute attribute) => Activator.CreateInstance(instance.ActivatorType, instance.RuntimeTypes.Evaluate(type, attribute));
/// <summary> /// Gets the property from the runtime type. /// </summary> /// <param name="runtimeType">Id</param> /// <value>Value</value> public static PropertyInfo GetPropertyInfo(IRuntimeType runtimeType) => _objects[Format.GetActivatorType(runtimeType)];
/// <summary> /// Sets the property to the runtime type. /// </summary> /// <param name="runtimeType">Id</param> /// <param name="value">Value</param> public static void SetPropertyInfo(IRuntimeType runtimeType, PropertyInfo value) => _objects[Format.GetActivatorType(runtimeType)] = value;
/// <summary> /// Creates the instance. /// </summary> /// <returns></returns> public object CreateInstance(IRuntimeType instance) => Activator.CreateInstance(instance.ActivatorType, instance.RuntimeTypes.Values(instance.Attribute, instance.Id));
/// <summary> /// Finds all dependency runtime types (instantiable types) which matches the criteria /// </summary> /// <param name="runtimeTypes">Types</param> /// <param name="typeParser">Parset</param> /// <param name="runtimeType">Id</param> /// <param name="args">Values</param> /// <returns></returns> public static IRuntimeType[] FindRuntimeTypes(this IEnumerable <IRuntimeType> runtimeTypes, ITypeParser typeParser, IRuntimeType runtimeType, object[] args) => typeParser.FindRuntimeTypes(runtimeType.TypeFullName, Format.GetNames(args), runtimeTypes.Where((p) => p.Attribute is IDependencyAttribute)).Where((p) => p.ActivatorType == runtimeType.ActivatorType).ToArray();
/// <summary> /// Matches the parameters. /// </summary> /// <param name="runtimeType">Type of the runtime.</param> /// <param name="name">The name.</param> /// <param name="args">The arguments.</param> /// <param name="match">The match.</param> /// <returns></returns> public static bool MatchParameters(this IRuntimeType runtimeType, string name, IEnumerable <string> args, MatchCollection match) => MatchType(runtimeType, name) && MatchRuntimeTypes(runtimeType, match.Cast <Match>().Select((p) => p.Value)) && MatchRuntimeTypes(runtimeType, args);
/// <summary> /// Matches the type. /// </summary> /// <param name="runtimeType">Type of the runtime.</param> /// <param name="name">The name.</param> /// <returns></returns> public static bool MatchType(this IRuntimeType runtimeType, string name) => runtimeType.TypeFullName == name;
/// <summary> /// Matches the parameter arguments. /// </summary> /// <param name="runtimeType">Type of the runtime.</param> /// <param name="args">The arguments.</param> /// <returns></returns> public static bool MatchRuntimeTypes(this IRuntimeType runtimeType, IEnumerable <string> args) { var count = args.Count(); return(count == 0 || (runtimeType.Count == count && ContainsTypeDefinition(runtimeType.RuntimeTypes, args))); }
/// <summary> /// Gets the <see cref="System.Type"/> of a class defined by /// <paramref name="runtime_type"/>. /// </summary> /// <param name="runtime_type"> /// A <see cref="IRuntimeType"/> object containing information about the /// type to be loaded. /// </param> /// <returns> /// The type instance that represents the exact runtime type of the /// specified provider or null if the type could not be loaded. /// </returns> /// <exception cref="ArgumentNullException"> /// <paramref name="runtime_type"/> is <c>null</c>. /// </exception> /// <exception cref="FileNotFoundException"> /// Tje assembly referenced by the <paramref name="runtime_type"/> is not /// found. /// </exception> /// <exception cref="PathTooLongException"> /// The path for the assembly referenced by the /// <paramref name="runtime_type"/> is too long. /// </exception> /// <remarks> /// We try to load the type's assembly using this location defined by the /// <see cref="IRuntimeType.Location"/> property of /// <paramref name="runtime_type"/> and them load the type from that /// assembly. /// </remarks> public static Type GetSystemType(IRuntimeType runtime_type) { if (runtime_type == null) throw new ArgumentNullException("runtime_type"); // Try to get the type from the loaded assemblies. Type type = System.Type.GetType(runtime_type.Type); // attempt to load .NET type of the provider. If the location of the // assembly is specified we need to load the assembly and try to get the // type from the loaded assembly. The name of the assembly will be // extracted from the provider type. if (type == null) { string assembly_name = runtime_type.Type; int num = assembly_name.IndexOf(','); if (num == -1) { // try to load the type from the calling assembly if a explicit // assembly was not specified. return Assembly.GetCallingAssembly().GetType(runtime_type.Type); } assembly_name = assembly_name.Substring(num + 1).Trim(); int num2 = assembly_name.IndexOfAny(new char[] {' ', ','}); if (num2 != -1) assembly_name = assembly_name.Substring(0, num2); string assembly_path = Path.Combine(runtime_type.Location, assembly_name); // If the file does not exists append one of the know extensions // to it and try again. if (!File.Exists(assembly_path)) { assembly_path = Path.Combine(runtime_type.Location, assembly_name) + ".dll"; if (!File.Exists(assembly_path)) { assembly_path = Path.Combine(runtime_type.Location, assembly_name) + ".exe"; } } Assembly assembly = Assembly.LoadFrom(assembly_path); type = assembly.GetType(runtime_type.Type.Substring(0, num)); } return type; }