CSLA.NET 5.4.2
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
20 {
24 public object Instance { get; private set; }
25
38 public LateBoundObject(Type objectType)
39 : this(MethodCaller.CreateInstance(objectType))
40 { }
41
49 public LateBoundObject(object instance)
50 {
51 this.Instance = instance;
52 }
53
61 public object CallMethodIfImplemented(string method)
62 {
63 return MethodCaller.CallMethodIfImplemented(this.Instance, method);
64 }
65
76 public object CallMethodIfImplemented(string method, params object[] parameters)
77 {
78 return MethodCaller.CallMethodIfImplemented(this.Instance, method, parameters);
79 }
80
89 public object CallMethod(string method)
90 {
91 return MethodCaller.CallMethod(this.Instance, method);
92 }
93
105 public object CallMethod(string method, params object[] parameters)
106 {
107 return MethodCaller.CallMethod(this.Instance, method, parameters);
108 }
109
117 public async Task CallMethodTryAsync(string methodName)
118 {
119 try
120 {
121 await MethodCaller.CallMethodTryAsync(this.Instance, methodName);
122 }
123 catch (CallMethodException)
124 {
125 throw;
126 }
127 catch (Exception ex)
128 {
129 throw new CallMethodException(Instance.GetType().Name + "." + methodName + " " + Resources.MethodCallFailed, ex);
130 }
131 }
132
142 public async Task CallMethodTryAsync(string methodName, params object[] parameters)
143 {
144 try
145 {
146 await MethodCaller.CallMethodTryAsync(this.Instance, methodName, parameters);
147 }
148 catch (CallMethodException)
149 {
150 throw;
151 }
152 catch (Exception ex)
153 {
154 throw new CallMethodException(Instance.GetType().Name + "." + methodName + " " + Resources.MethodCallFailed, ex);
155 }
156 }
157
167 public async Task CallMethodTryAsyncDI<T>(bool isSync, params object[] parameters)
169 {
170 var method = ServiceProviderMethodCaller.FindDataPortalMethod<T>(
171 Instance, parameters);
172 try
173 {
174 Utilities.ThrowIfAsyncMethodOnSyncClient(isSync, method.MethodInfo);
175 await ServiceProviderMethodCaller.CallMethodTryAsync(Instance, method, parameters).ConfigureAwait(false);
176 }
177 catch (CallMethodException)
178 {
179 throw;
180 }
181 catch (Exception ex)
182 {
183 throw new CallMethodException(Instance.GetType().Name + "." + method.MethodInfo.Name + " " + Resources.MethodCallFailed, ex);
184 }
185 }
186 }
187}
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.