Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Including Castle DynamicProxy possible?

Discussion in 'Scripting' started by seaders, Feb 5, 2015.

  1. seaders

    seaders

    Joined:
    Oct 28, 2014
    Posts:
    29
    I need to build a proxy class not dissimilar to

    Code (CSharp):
    1. using System.Reflection;
    2.  
    3. namespace SixMinute.ProxySample
    4. {
    5.     using System;
    6.     using System.Collections.Generic;
    7.     using Castle.DynamicProxy;
    8.  
    9.     public class Program
    10.     {
    11.         static void Main(string[] args)
    12.         {
    13.             Pet rex = Something.MakeSomething<Pet>();
    14.  
    15.             rex.Name = "Rex";
    16.             rex.ToString();
    17.             rex.Age += 50;
    18.  
    19.             rex.Age++;
    20.         }
    21.     }
    22.  
    23.     public class Pet
    24.     {
    25.         public virtual string Other { get; set; }
    26.         public virtual string Name { get; set; }
    27.         public virtual int Age { get; set; }
    28.  
    29.         public override string ToString()
    30.         {
    31.             return string.Format("Name: {0}, Age: {1}", Name, Age);
    32.         }
    33.  
    34.         public object getFieldDirect(string name)
    35.         {
    36.             return GetType().BaseType.GetField(name, BindingFlags.Instance|BindingFlags.NonPublic).GetValue(this);
    37.         }
    38.     }
    39.  
    40.     public static class Something
    41.     {
    42.         private static readonly ProxyGenerator _generator = new ProxyGenerator(new PersistentProxyBuilder());
    43.  
    44.         public static TSomething MakeSomething<TSomething>() where TSomething : class, new()
    45.         {
    46.             TSomething proxy = _generator.CreateClassProxy<TSomething>( new MyInterceptor() );
    47.             return proxy;
    48.         }
    49.     }
    50.  
    51.     [Serializable]
    52.     public class MyInterceptor : IInterceptor
    53.     {
    54.         #region IInterceptor Members
    55.  
    56.         private Dictionary<string, string> dict = new Dictionary<string, string>();
    57.  
    58.         public void Intercept(IInvocation invocation)
    59.         {
    60.             invocation.Proceed();
    61.  
    62.             if( invocation.Method.Name.StartsWith("set_") )
    63.             {
    64.                 string shortName = invocation.Method.Name.Substring(4);
    65.                 string intName = "<" + shortName + ">k__BackingField";
    66.                 object obj = invocation.InvocationTarget;
    67.                 BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
    68.                 FieldInfo fi = invocation.TargetType.GetField(intName, flags);
    69.                 Console.WriteLine( shortName + ": " + fi + " - " + fi.GetValue(obj) );
    70.             }
    71.         }
    72.  
    73.         #endregion
    74.     }
    75.  
    76. }
    77.  
    But that relies on the Castle DynamicProxy libraries, http://www.castleproject.org/projects/dynamicproxy/ which first off, has 6 options,
    • net35
    • net40
    • net40-client
    • net45
    • sl4
    • sl5
    Which first off, I don't actually know which to use, and secondly none work straight off when I drag it in to somewhere in the Assets folder.

    If I instead of importing just the dlls, take in the source, things the compiler starts complaining about all sorts of things, I think specifically "System.Dynamic" and in and around the "DynamicObject" bits and pieces.

    Anyone had any luck getting them working with Unity?
     
  2. Alanisaac

    Alanisaac

    Joined:
    Jan 25, 2015
    Posts:
    15
    Unity doesn't use c# .NET, but rather it uses Mono. It should be effectively equivalent to .NET 3.5, but even saying that doesn't quite do the answer justice. I don't think the castle library is compatible with Mono in general, unfortunately.

    I've dabbled in Castle in other projects (c#/WPF UI, for example), but you're going to run into a big roadblock with dynamics. The dynamic keyword wasn't even introduced until .NET 4.0. As you've seen, the Castle Dynamic Proxy libraries have a lot of those dependencies.

    If there's a .NET 3.5 implementation of the library, that would be your best bet on a place to start to try to port it, but it might take way too much effort for the gain.
     
  3. seaders

    seaders

    Joined:
    Oct 28, 2014
    Posts:
    29
    That definitely is what I unfortunately thought alright, but there is that net35 folder of Dlls which just give the stacktrace,

    Which doesn't really point to what might be going wrong.

    Ah well, I'll have to just start poking around with Cecil to get him working :/