How do I inject an extension method ONLY into the instanced class?
If you're talking about adding an extension method to individual instances, you can't. There's no such concept in C#. An extension method "extends" the type, not individual instances of the type. How would you expect the compiler to know which instances had the extension applied to it at compile-time, if that knowledge is only available at execution-time?
If you're talking about calling an extension method like any other static method, that will always be feasible, and can't be prevented. So you could always call:
MyExtensionClass.Add(null, null);
If you're not talking about either of those things, it's not clear what you mean - but making a static call to ArrayList.Add
as shown in your sample code really won't compile. Are you sure you haven't got a variable called ArrayList
? (That would certainly mess things up.)
Furthermore, as ArrayList
already has an Add(object)
instance method, your new extension method would never be used anyway. (You should really move away from the non-generic collections as quickly as you can.)