Hello,
say I have an object and I want to obtain a new one whose class is a
mixin of the object's class and some simple implementations of two
interfaces.
Here is what I do :
[code]
using System;
using System.Reflection;
using Castle.DynamicProxy;
namespace Test
{
public interface IA
{
string SA
{
get;
set;
}
}
public interface IB
{
string SB
{
get;
set;
}
}
public class ADefaultImplementation : IA
{
public string SA
{
get;
set;
}
}
public class BDefaultImplementation : IB
{
public string SB
{
get;
set;
}
}
public class C
{
}
public class Application
{
private static void Main()
{
ProxyGenerator _proxyGenerator = new ProxyGenerator();
// Creation of the first mixin instance "mixin1 = C +
ADefaultImplementation"
ProxyGenerationOptions optionsA = new
ProxyGenerationOptions();
optionsA.AddMixinInstance(new ADefaultImplementation());
IA a = _proxyGenerator.CreateClassProxy(typeof(C), null,
optionsA) as IA;
// Creation of the second mixin instance "mixin2 = mixin1
+ BDefaultImplementation"
ProxyGenerationOptions optionsB = new
ProxyGenerationOptions();
optionsB.AddMixinInstance(new BDefaultImplementation());
IB b = _proxyGenerator.CreateClassProxy(a.GetType(), null,
optionsB) as IB;
Console.WriteLine(b.SB); // OK
Console.WriteLine((b as IA).SA); // KO
}
}
}
[/code]
But trying to access "SA" raises the following exception :
[code]
System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."
Source="DynamicProxyGenAssembly2"
StackTrace:
at
CProxy43c7f990b4df48bfaa238358baeb0bcf.Invocationget_SA_1.InvokeMethodOnTarget
()
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Castle.Core.Interceptor.StandardInterceptor.Intercept
(IInvocation invocation)
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at CProxy43c7f990b4df48bfaa238358baeb0bcf.get_SA()
at
CProxy43c7f990b4df48bfaa238358baeb0bcfProxy3475609451fe4a69beb6ace21633e3e2.get_SA_callback_4
()
at
CProxy43c7f990b4df48bfaa238358baeb0bcfProxy3475609451fe4a69beb6ace21633e3e2.Invocationget_SA_4.InvokeMethodOnTarget
()
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at
CProxy43c7f990b4df48bfaa238358baeb0bcfProxy3475609451fe4a69beb6ace21633e3e2.get_SA
()
at Test.Application.Main() in H:\p\test\wpf\TestForum\TestForum
\Program.cs:line 67
at System.AppDomain._nExecuteAssembly(Assembly assembly, String
[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile,
Evidence assemblySecurity, String[] args)
at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object
state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
[/code]
Unfortunately notice that I can't use some basic working workarounds
like :
- using a unique mixin base "ABDefaultImplementation" with the two
fields "SA" and "SB" this way :
[code]
public class ABDefaultImplementation : IA, IB
{
public string SA
{
get;
set;
}
public string SB
{
get;
set;
}
}
...
ProxyGenerationOptions optionsAB = new ProxyGenerationOptions();
optionsAB.AddMixinInstance(new ABDefaultImplementation());
IB ab = _proxyGenerator.CreateClassProxy(typeof(C), null, optionsAB)
as IB;
[/code]
- generating the final mixin object with a unique call to
"CreateClassProxy" :
[code]
ProxyGenerationOptions optionsAB = new ProxyGenerationOptions();
optionsAB.AddMixinInstance(new ADefaultImplementation());
optionsAB.AddMixinInstance(new BDefaultImplementation());
IB ab = _proxyGenerator.CreateClassProxy(typeof(C), null, optionsAB)
as IB;
[/code]
Am I doing something wrong, is it a bug, or a known limitation ?
The used version of "DynamicProxy" is 2.1.
Thanks by advance for your help.
Best regards.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Castle Project Users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/castle-project-users?hl=en
-~----------~----~----~----~------~----~------~--~---