C# Client Library
A C# Client Library for the AnalyzeRe REST API
Loading...
Searching...
No Matches
ExtensionMethods.Reference.cs
Go to the documentation of this file.
1using System;
3
4// ReSharper disable once CheckNamespace (Extension methods work best on the root namespace)
5namespace AnalyzeRe
6{
8 public static partial class ExtensionMethods
9 {
10 private static readonly Type _referenceGenericBaseType = typeof(Reference<>);
11
19 public static IReference<T> ToReference<T>(this T obj, bool stronglyTyped = false) where T : IAPIResource
20 {
21 if (obj == null)
22 throw new ArgumentNullException(nameof(obj),
23 "Attempted to call '.toReference()' on a null resource.");
24 if (!stronglyTyped)
25 return new Reference<T>(obj);
26 // Make sure to get the run-time type of the object we are converting to a reference.
27 Type runtimeType = obj.GetType();
28 if (runtimeType == typeof(T))
29 return new Reference<T>(obj);
30 // Slower, but ensures the reference of the most specific type possible.
31 // Use a cache to avoid having to make the corresponding generic type each time.
32 Type runtime_reference_type = _referenceGenericBaseType.MakeGenericTypeFast(runtimeType);
33 return (IReference<T>)Activator.CreateInstance(runtime_reference_type,
34 new object[] { obj });
35 }
36 }
37}
Provides the REST request methods available to data endpoints.
static IReference< T > ToReference< T >(this T obj, bool stronglyTyped=false)
Converts this IAPIResource object into a strongly typed reference to that object.
Implements the reference entity interface, with support for resolving references using lazy loading.
Interface for Base class used by all resources.
Base interface for all reference entities.