Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Help] I can't use Castle for proxy internal class #686

Open
Dragon-0609 opened this issue Sep 11, 2024 · 7 comments
Open

[Help] I can't use Castle for proxy internal class #686

Dragon-0609 opened this issue Sep 11, 2024 · 7 comments

Comments

@Dragon-0609
Copy link

Hi. I have an internal class in another assemly.

internal class StatusBar : View

I want to override it's protected override void OnDisplay(), but, for some reason my code doesn't work.

Here's my code

private static void Hook(Type targetType, object instance ){
	ProxyGenerator generator = new ProxyGenerator();

	var proxy = generator.CreateClassProxyWithTarget(
		targetType, // The type of the class to proxy
		instance, // The instance of the internal class
		new OnDisplayInterceptor() // Our interceptor to handle the method call
	);
	var methodInfo = proxy.GetType().GetMethod("OnDisplay", BindingFlags.NonPublic | BindingFlags.Instance);
	methodInfo.Invoke(proxy, null); // Call the method via reflection
}

public class OnDisplayInterceptor : IInterceptor
{
	public void Intercept(IInvocation invocation)
	{
		Console.WriteLine("Before OnDisplay");

		invocation.Proceed();

		Console.WriteLine("Updated OnDisplay");
	}
}

Before OnDisplay is printed, but invocation.Proceed doesn't work. It gives null reference. Do you know in where I wrote wrong?

@stakx
Copy link
Member

stakx commented Sep 11, 2024

You'd be better off asking plain usage questions on places such as Stack Overflow; this GitHub issue tracker focuses on the development of this library.

Even if this were a bug, your code example is incomplete – you've not included the relevant method of the proxied type.

All of that being said, I'm assuming the problem here might be a missing assembly: InternalsVisibleTo(InternalsVisible.ToDynamicProxyGenAssembly2)] attribute in the assmbly that first declared View.OnDisplay. Have you tried adding that attribute to your assembly and overriding that method in your derived class?

@Dragon-0609
Copy link
Author

Dragon-0609 commented Sep 11, 2024

the problem here might be a missing assembly: InternalsVisibleTo(InternalsVisible.ToDynamicProxyGenAssembly2)] attribute in the assmbly that first declared View.OnDisplay

If I had access to change the library, I would have directly inherited the class itself and override it.

Even if this were a bug, your code example is incomplete – you've not included the relevant method of the proxied type.

What's the relevant method of the proxied type? I asked Chat GPT to help me with my problem and it suggested the code similar to above

@stakx
Copy link
Member

stakx commented Sep 11, 2024

The "relevant method of the proxied type" is the one you are interested in. (I'm guessing View.OnDisplay but I cannot be sure since your repro code above is incomplete.)

@Dragon-0609
Copy link
Author

Do you need full code of View.OnDisplay?

@stakx
Copy link
Member

stakx commented Sep 11, 2024

Anyhow, we can leave this issue open for a while, in case someone else wants to chime in and offer help; otherwise I'll close it in a few weeks or so.

@Dragon-0609
Copy link
Author

Dragon-0609 commented Sep 11, 2024

Ok, thanks for your reply. In case you asked is the method virtual or abstract, it's virtual.
Here's the definition from View:

    protected virtual void OnDisplay()
    {
    }

@stakx
Copy link
Member

stakx commented Sep 11, 2024

Do you need full code of View.OnDisplay?

No, not necessarily. If you seek help online, you should show just enough of your code such that it can be compiled and executed successfully while still exhibiting the problem that you're asking about... don't just drop all of your code without making any attempt to remove all irrelevant bits first... do try to come up with a minimal repro. (The Stack Overflow community used to call that a minimally complete and verifiable code example.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants