public static LookupInstructions Load(string resourcePath, Func <LookupInstructions> getDefault = null) { var isEditor = Application.isEditor && !Application.isPlaying; var assetPath = isEditor ? Path.Combine(ConstFolder.ASSETS_RESOURCES, resourcePath) : resourcePath; var textAsset = isEditor ? AssetDatabase.LoadAssetAtPath <TextAsset>(assetPath) : Resources.Load <TextAsset>(Path.Combine(Path.GetDirectoryName(assetPath), Path.GetFileNameWithoutExtension(assetPath))); var instructions = (LookupInstructions)null; if (textAsset == null) { instructions = new LookupInstructions(getDefault.SafeInvoke()); } else { instructions = JsonUtility.FromJson <LookupInstructions>(textAsset.text); } instructions.Init(isEditor); return(instructions); }
public static void Run(Func <IDisposable> allocationCode, Action <IDisposable> bodyCode) { IDisposable resource = null; try { try { //Empty } finally { resource = allocationCode.SafeInvoke(null); } if (resource != null) { bodyCode.SafeInvoke(resource); } } finally { if (resource != null) { resource.Dispose(); } } }
public static TReturn SafeWith <T, TReturn>(this T obj, Func <T, TReturn> action, Func <TReturn> alternative, TReturn def) { if (!ReferenceEquals(obj, null)) { return(action.SafeInvoke(obj, alternative, def)); } else { return(alternative.SafeInvoke(def)); } }
/// <inheritdoc/> public T ResolveDependency <T>(Func <T> objectFactory) { if (this._dependencies.TryGetValue(typeof(T), out var dependency)) { return((T)dependency); } dependency = objectFactory.SafeInvoke(); this._dependencies.Add(typeof(T), dependency); return((T)dependency); }
/// <summary> /// Get a binding template for the given binding key. /// </summary> /// <param name="bindingKey">The registered binding key.</param> /// <returns>A binding template. Returns null if no template is found.</returns> private static BindingTemplate GetBindingTemplate(string bindingKey) { // The default UDDI inquiry service entry in the configuration file. var defaultDirectoryEntry = Cache.SingleOrDefault(entry => entry.Key == DefaultUddiInquiryService); // The first binding template, if found. Func <UddiSiteLocation, BindingTemplate> getBindingTemplate = // Search a site location for a binding template: siteLocation => new GetBindingDetail(bindingKey).Send(new UddiConnection(siteLocation)) .BindingTemplates.FirstOrDefault(); // Search for and return the business service: return // Search the default directory for the binding template... (getBindingTemplate.SafeInvoke(string.Format("Binding key: {0}", bindingKey))( defaultDirectoryEntry.Value) ?? // ...and if nothing is found, search through all discovered directories. (from entry in Cache where entry.Key != ControlCacheRefreshEntry && entry.Key != DefaultUddiInquiryService select getBindingTemplate.SafeInvoke(string.Format("Binding key: {0}", bindingKey))(entry.Value)) .FirstOrDefault(bindingTemplate => bindingTemplate != null)); }
/// <summary> /// Gets the value associated with this property. /// </summary> /// <typeparam name="T">The type of the property to get</typeparam> /// <param name="preceeding">Action to invoke before the property value is retrieved</param> /// <param name="following">Action to be invoked after the property value is retrieved. Recieves the retrieved value as an argument</param> /// <param name="modifier">Used to modify the returned value without actually setting the underlying property</param> /// <param name="propertyName">The name of the property to retrieve. Supplied by CallerMemberName</param> /// <returns>The value of the given property</returns> protected T GetProperty <T>(Action preceeding, Action <T> following, Func <T, T> modifier, [CallerMemberName] string propertyName = "") { // func to simplify control flow Func <T> getValue; // we need to get the property, which may either exist in one of the registered models, // in our internal properties dictionary, or not exist at all. If it does not exist, // we need to also create it, since we cannot gaurentee that SetProperty will be called prior var models = m_propertyModels.Where(o => o.GetType().GetProperties() .Where(p => p.Name == propertyName && p.PropertyType == typeof(T)).Any()); if (models.Any()) { // property exists on model, retrieve it var model = models.Single(); var prop = model.GetType().GetProperty(propertyName); if (prop == null) { throw new InvalidOperationException("Could not retrieve expected model property: " + propertyName); } getValue = (() => (T)prop.GetValue(model)); } else { // property does not exist on model object res; if (!m_properties.TryGetValue(propertyName, out res)) { m_properties.Add(propertyName, default(T)); return(default(T)); } var prop = this.GetType().GetProperty(propertyName); if (prop == null) { throw new InvalidOperationException("Could not retrieve expected model property: " + propertyName); } if (!prop.PropertyType.IsAssignableFrom(typeof(T))) { throw new ArgumentException("Property type cannot be assigned to expected type: " + typeof(T).FullName); } getValue = (() => (T)res); } // invoke our delegates in the apropriate order preceeding.SafeInvoke(); T retVal = getValue(); following.SafeInvoke(retVal); retVal = modifier.SafeInvoke(retVal); return(retVal); }
/// <summary> /// Returns a UDDI business service for the given site location, treating the /// string as a service key. /// </summary> /// <param name="serviceKey">The service key.</param> /// <param name="siteLocation">The UDDI site location.</param> /// <returns>The UDDI business service.</returns> public static BusinessService UddiBusinessServiceByKey(this string serviceKey, UddiSiteLocation siteLocation) { // The UDDI business service for the given key. Func <BusinessService> uddiBusinessServiceByKey = () => new GetServiceDetail(serviceKey) .Send(new UddiConnection(siteLocation)) .BusinessServices .FirstOrDefault(); return(uddiBusinessServiceByKey.SafeInvoke( siteLocation, string.Format("UDDI service key: {0}", serviceKey))()); }
/// <summary> /// Returns a UDDI business entity for the given site location, treating the /// string as a business key. /// </summary> /// <param name="businessKey">The business entity key.</param> /// <param name="siteLocation">The UDDI site location.</param> /// <returns>The UDDI business entity.</returns> public static BusinessEntity UddiBusinessEntityByKey(this string businessKey, UddiSiteLocation siteLocation) { // The UDDI business entity for the given key. Func <BusinessEntity> uddiBusinessEntityByKey = () => new GetBusinessDetail(businessKey) .Send(new UddiConnection(siteLocation)) .BusinessEntities .FirstOrDefault(); return(uddiBusinessEntityByKey.SafeInvoke( siteLocation, string.Format("UDDI business key: {0}", businessKey))()); }
public static TReturn DisposableSafeWith <T, TReturn>(this T disposable, Func <T, TReturn> action, Func <TReturn> alternative, TReturn def) where T : IExtendedDisposable { if (!ReferenceEquals(disposable, null)) { return(disposable.DisposedConditional ( null, () => action.SafeInvoke(disposable, alternative, def) )); } else { return(alternative.SafeInvoke(def)); } }
/// <summary> /// Initializes a new instance of the <see cref="Bootstrapper"/> class. /// </summary> public Bootstrapper(IRunnableFactory runnableFactory, IDescriptorRepository descriptorRepository, Func<Bootstrapper, ICommandDescriptorManager> commandDescriptorManager = null, Func<Bootstrapper, ICommandCallResolver> commandResolver = null, Func<Bootstrapper, ISelectorFactory> selectorFactory = null, Func<Bootstrapper, ISelectorResolver> selectorResolver = null, Func<Bootstrapper, IRunnableManager> runnableManager = null, Func<Bootstrapper, IExecutor> executor = null, Func<Bootstrapper, IControlFlowFactory<ControlFlowBase>> controlFlowFactory = null) { Assume.NotNull(runnableFactory, nameof(runnableFactory)); Assume.NotNull(descriptorRepository, nameof(descriptorRepository)); RunnableFactory = runnableFactory; DescriptorRepository = descriptorRepository; CommandDescriptorManager = commandDescriptorManager.SafeInvoke(this) ?? new DefaultCommandDescriptorManager(); CommandCallResolver = commandResolver.SafeInvoke(this) ?? new DefaultCommandCallResolver(descriptorRepository, RunnableFactory); SelectorFactory = selectorFactory.SafeInvoke(this) ?? new DefaultSelectorFactory(); SelectorResolver = selectorResolver.SafeInvoke(this) ?? new DefaultSelectorResolver(SelectorFactory, DescriptorRepository); RunnableManager = runnableManager.SafeInvoke(this) ?? new DefaultRunnableManager(); Executor = executor.SafeInvoke(this) ?? new StandardExecutor(RunnableManager); ControlFlowFactory = controlFlowFactory.SafeInvoke(this) ?? new DefaultControlFlowFactory(CommandCallResolver, SelectorResolver, Executor); Compiler = new CodeQueryCompiler(); }
/// <summary> /// Initializes a new instance of the <see cref="Bootstrapper"/> class. /// </summary> public Bootstrapper(IRunnableFactory runnableFactory, IDescriptorRepository descriptorRepository, Func <Bootstrapper, ICommandDescriptorManager> commandDescriptorManager = null, Func <Bootstrapper, ICommandCallResolver> commandResolver = null, Func <Bootstrapper, ISelectorFactory> selectorFactory = null, Func <Bootstrapper, ISelectorResolver> selectorResolver = null, Func <Bootstrapper, IRunnableManager> runnableManager = null, Func <Bootstrapper, IExecutor> executor = null, Func <Bootstrapper, IControlFlowFactory <ControlFlowBase> > controlFlowFactory = null) { Assume.NotNull(runnableFactory, nameof(runnableFactory)); Assume.NotNull(descriptorRepository, nameof(descriptorRepository)); RunnableFactory = runnableFactory; DescriptorRepository = descriptorRepository; CommandDescriptorManager = commandDescriptorManager.SafeInvoke(this) ?? new DefaultCommandDescriptorManager(); CommandCallResolver = commandResolver.SafeInvoke(this) ?? new DefaultCommandCallResolver(descriptorRepository, RunnableFactory); SelectorFactory = selectorFactory.SafeInvoke(this) ?? new DefaultSelectorFactory(); SelectorResolver = selectorResolver.SafeInvoke(this) ?? new DefaultSelectorResolver(SelectorFactory, DescriptorRepository); RunnableManager = runnableManager.SafeInvoke(this) ?? new DefaultRunnableManager(); Executor = executor.SafeInvoke(this) ?? new StandardExecutor(RunnableManager); ControlFlowFactory = controlFlowFactory.SafeInvoke(this) ?? new DefaultControlFlowFactory(CommandCallResolver, SelectorResolver, Executor); Compiler = new CodeQueryCompiler(); }
public T SafeInvoke() { return(act.SafeInvoke()); }