CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
LateBoundObject.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="LateBoundObject.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Enables simple invocation of methods</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Threading.Tasks;
10using Csla.Properties;
11
12namespace Csla.Reflection
13{
19 public class LateBoundObject : Core.IUseApplicationContext
20 {
21 private ApplicationContext ApplicationContext { get; set; }
22 ApplicationContext Core.IUseApplicationContext.ApplicationContext { get => ApplicationContext; set => ApplicationContext = value; }
23
27 public object Instance { get; private set; }
28
41 public LateBoundObject(Type objectType)
42 {
44 }
45
53 public LateBoundObject(object instance)
54 {
55 Instance = instance;
56 }
57
65 public object CallMethodIfImplemented(string method)
66 {
67 return MethodCaller.CallMethodIfImplemented(this.Instance, method);
68 }
69
80 public object CallMethodIfImplemented(string method, params object[] parameters)
81 {
82 return MethodCaller.CallMethodIfImplemented(this.Instance, method, parameters);
83 }
84
93 public object CallMethod(string method)
94 {
95 return MethodCaller.CallMethod(this.Instance, method);
96 }
97
109 public object CallMethod(string method, params object[] parameters)
110 {
111 return MethodCaller.CallMethod(this.Instance, method, parameters);
112 }
113
121 public async Task CallMethodTryAsync(string methodName)
122 {
123 try
124 {
125 await MethodCaller.CallMethodTryAsync(this.Instance, methodName);
126 }
127 catch (CallMethodException)
128 {
129 throw;
130 }
131 catch (Exception ex)
132 {
133 throw new CallMethodException(Instance.GetType().Name + "." + methodName + " " + Resources.MethodCallFailed, ex);
134 }
135 }
136
146 public async Task CallMethodTryAsync(string methodName, params object[] parameters)
147 {
148 try
149 {
150 await MethodCaller.CallMethodTryAsync(this.Instance, methodName, parameters);
151 }
152 catch (CallMethodException)
153 {
154 throw;
155 }
156 catch (Exception ex)
157 {
158 throw new CallMethodException(Instance.GetType().Name + "." + methodName + " " + Resources.MethodCallFailed, ex);
159 }
160 }
161
162 private Reflection.ServiceProviderMethodCaller serviceProviderMethodCaller;
163 private Reflection.ServiceProviderMethodCaller ServiceProviderMethodCaller
164 {
165 get
166 {
167 if (serviceProviderMethodCaller == null)
168 serviceProviderMethodCaller = (Reflection.ServiceProviderMethodCaller)ApplicationContext.CreateInstanceDI(typeof(Reflection.ServiceProviderMethodCaller));
169 return serviceProviderMethodCaller;
170 }
171 }
172
182 public async Task CallMethodTryAsyncDI<T>(bool isSync, params object[] parameters)
184 {
185 var method = ServiceProviderMethodCaller.FindDataPortalMethod<T>(
186 Instance, parameters);
187 try
188 {
189 Utilities.ThrowIfAsyncMethodOnSyncClient(ApplicationContext, isSync, method.MethodInfo);
190 await ServiceProviderMethodCaller.CallMethodTryAsync(Instance, method, parameters).ConfigureAwait(false);
191 }
192 catch (CallMethodException)
193 {
194 throw;
195 }
196 catch (Exception ex)
197 {
198 throw new CallMethodException(Instance.GetType().Name + "." + method.MethodInfo.Name + " " + Resources.MethodCallFailed, ex);
199 }
200 }
201 }
202}
Provides consistent context information between the client and server DataPortal objects.
object CreateInstanceDI(Type objectType, params object[] parameters)
Creates an object using 'Activator.CreateInstance' using service provider (if one is available) to po...
ApplicationContext(ApplicationContextAccessor applicationContextAccessor)
Creates a new instance of the type
Base type for data portal operation attributes.
A strongly-typed resource class, for looking up localized strings, etc.
static string MethodCallFailed
Looks up a localized string similar to method call failed.
This exception is returned from the CallMethod method in the server-side DataPortal and contains the ...
Enables simple invocation of methods against the contained object using late binding.
async Task CallMethodTryAsync(string methodName, params object[] parameters)
Invokes a method using the await keyword if the method returns Task, otherwise synchronously invokes ...
LateBoundObject(object instance)
Contains the provided object within a new LateBoundObject.
object CallMethodIfImplemented(string method, params object[] parameters)
Uses reflection to dynamically invoke a method if that method is implemented on the target object.
async Task CallMethodTryAsync(string methodName)
Invokes a method using the await keyword if the method returns Task, otherwise synchronously invokes ...
object CallMethod(string method)
Uses reflection to dynamically invoke a method, throwing an exception if it is not implemented on the...
object CallMethodIfImplemented(string method)
Uses reflection to dynamically invoke a method if that method is implemented on the target object.
async Task CallMethodTryAsyncDI< T >(bool isSync, params object[] parameters)
Invokes a method using the await keyword if the method returns Task, otherwise synchronously invokes ...
LateBoundObject(Type objectType)
Creates an instance of the specified type and contains it within a new LateBoundObject.
object CallMethod(string method, params object[] parameters)
Uses reflection to dynamically invoke a method, throwing an exception if it is not implemented on the...
object Instance
Object instance managed by LateBoundObject.
Methods to dynamically find/invoke methods with data portal and DI provided params
async Task< object > CallMethodTryAsync(object obj, ServiceProviderMethodInfo method, object[] parameters)
Invoke a method async if possible, providing parameters from the params array and from DI