Reflection method for determining valid interfaces

This commit is contained in:
Cyberboss
2017-10-18 13:00:31 -04:00
parent 4cb95a0ea5
commit d72dde6886
+22 -3
View File
@@ -1,10 +1,13 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Reflection;
using System.Security.Principal;
using System.ServiceModel;
namespace TGServiceInterface
{
public class Server
@@ -12,7 +15,7 @@ namespace TGServiceInterface
/// <summary>
/// List of types that can be used with GetComponen
/// </summary>
public static readonly IList<Type> ValidInterfaces = new List<Type> { typeof(ITGByond), typeof(ITGChat), typeof(ITGCompiler), typeof(ITGConfig), typeof(ITGDreamDaemon), typeof(ITGRepository), typeof(ITGSService), typeof(ITGConnectivity), typeof(ITGAdministration), typeof(ITGInterop) };
public static readonly IList<Type> ValidInterfaces = CollectComponents();
/// <summary>
/// The maximum message size to and from a local server
@@ -51,6 +54,22 @@ namespace TGServiceInterface
static string HTTPSPassword;
static Dictionary<Type, ChannelFactory> ChannelFactoryCache = new Dictionary<Type, ChannelFactory>();
/// <summary>
/// Returns a <see cref="IList{T}"/> of <see langword="interface"/> <see cref="Type"/>s that can be used with the service
/// </summary>
/// <returns>A <see cref="IList{T}"/> of <see langword="interface"/> <see cref="Type"/>s that can be used with the service</returns>
static IList<Type> CollectComponents()
{
//find all interfaces in this assembly in this namespace that have the service contract attribute
var query = from t in Assembly.GetExecutingAssembly().GetTypes()
where t.IsInterface
&& t.Namespace == typeof(Server).Namespace
&& t.GetCustomAttribute(typeof(ServiceContractAttribute)) != null
select t;
return query.ToList();
}
public static void SetBadCertificateHandler(Func<string, bool> handler)
{
ServicePointManager.ServerCertificateValidationCallback = (sender, cert, chain, error) =>
@@ -97,8 +116,8 @@ namespace TGServiceInterface
public static bool VersionMismatch(out string errorMessage)
{
var splits = Server.GetComponent<ITGSService>().Version().Split(' ');
var theirs = new Version(splits[splits.Length - 1].Substring(1));
var splits = Server.GetComponent<ITGSService>().Version().Split(' ');
var theirs = new Version(splits[splits.Length - 1].Substring(1));
var ours = new Version(FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).FileVersion);
if(theirs != ours)
{