target .net framework 4.8.1
In fact, this is the minimal implementation code for a part of a more complex project.
Due to the type being changed to System.Object, code encountered issues in build 1879.
Actually, I heavily rely on reflection and proxies.
Code: Select all
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Messaging;
namespace test1
{
internal class Program
{
static void Main(string[] args)
{
var service = new Service();
var proxy = new Proxy(service);
var interfaceProxy = (IInterface)proxy.GetTransparentProxy();
interfaceProxy.DoSomething();
Console.Read();
}
public class Proxy : RealProxy
{
private IInterface _target;
public Proxy(IInterface target) : base(typeof(IInterface))
{
_target = target;
}
public override IMessage Invoke(IMessage msg)
{
if (msg is IMethodCallMessage methodCall)
{
Console.WriteLine("Proxy is intercepting the method call.");
Console.WriteLine(methodCall.TypeName.ToString());
var result = methodCall.MethodBase.Invoke(_target, methodCall.InArgs);
Console.WriteLine("Proxy has called the service class method.");
return new ReturnMessage(result, null, 0, methodCall.LogicalCallContext, methodCall);
}
return null;
}
}
}
public class Service : MarshalByRefObject, IInterface
{
public void DoSomething()
{
Console.WriteLine("Service is doing something.");
}
}
public interface IInterface
{
void DoSomething();
}
}
Code: Select all
<?xml version="1.0" encoding="UTF-8" ?>
<Document Version="2">
<Protection InputFileName="test1.exe" Options="70400" VMCodeSectionName=".???" VMComplexity="20">
<Messages />
<Folders />
<Procedures>
<Procedure MapAddress="test1.Program::Main(string[])" Options="0" CompilationType="2" />
<Procedure MapAddress="test1.Program::.ctor()" Options="0" CompilationType="2" />
<Procedure MapAddress="test1.Service::DoSomething()" Options="0" CompilationType="2" />
<Procedure MapAddress="test1.Service::.ctor()" Options="0" CompilationType="2" />
<Procedure MapAddress="test1.Program/Proxy::.ctor(class test1.IInterface)" Options="0" CompilationType="2" />
<Procedure MapAddress="test1.Program/Proxy::Invoke(class System.Runtime.Remoting.Messaging.IMessage)" Options="0" CompilationType="2" />
</Procedures>
<Objects />
</Protection>
<Script />
</Document>
Code: Select all
Proxy is intercepting the method call.
test1.IInterface, test1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
Service is doing something.
Proxy has called the service class method.
Code: Select all
Proxy is intercepting the method call.
System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Proxy has called the service class method.
Proxy is intercepting the method call.
test1.IInterface, test1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
Service is doing something.
Proxy has called the service class method.