Question

I have a sample class:

class SampleClass
{
   public virtual string SomeProperty{get; set;}
   public virtual void SomeMethod() {
      // code
   }
}

I can Inherit from it and override SomeProperty and SomeMethod like this:

class ChildClass:SampleClass
{
   public override string SomeProperty{get; set;}
   public override void SomeMethod() {
      // code
   }
}

Is there any way I can override from a object, not from a class? Like

SampleClass sampleObject = new sampleObject();

And have sampleObject.SomeMethod() be unique? Thanks.

Was it helpful?

Solution

No, you can't override on a per-object basis. However, if you want to give different objects different behaviour models, you could always pass in a delegate to the constructor:

public class ChildClass : SampleClass
{
   private readonly Action someMethodBehavior;

   public ChildClass(Action someMethodBehavior) {
       this.someMethodBehavior = someMethodBehavior;
   }

   public override void SomeMethod() {
      someMethodBehavior();
   }
}

That allows each object to effectively specify its own behavior. You could even allow a null delegate reference to mean "just perform the default action" if you wanted it to be a sort of opt-in override.

OTHER TIPS

Not entirely sure what you're trying to accomplish, but you would just do

SampleClass sampleObject = new ChildClass();

Then you could pass sampleObject to any function that took a SampleClass, even though it actually is a ChildClass, and since your functions are virtual, your overridden functions would be called.

No - there is no way that you can override from an object! In fact I think that you misunderstand what and object is- an object is an instance of a class. An object its self doesnt have defined methods and properties, they come from the class to which the object 'belongs'.

Sounds to me that if you need to try and do this sort of thing then you are going down the wrong path...

Your question is a bit unclear, but I think you can't exactly do what you want - anonymous classes don't exist in C# like in Java. You would need to create a subclass & method override for that instance. You could make the subclass private within the class you're declaring the code in, that way it could only be accessed as a SampleClass, and not a ChildClass

Sounds like you're possibly going about something the wrong way and I'm not sure why you wouldn't create a child class with an overridden SomeMethod() method, but...

The only way might be to explicitly create the class with this in mind, keeping a delegate property. Then SomeMethod() would call the delegate (presumably set when the object is created).

The delegate still wouldn't be allowed access to private or protected members, however - without some clever argument passing at least - so I don't really see much use.

No. This isn't possible in C#. You'd have to create a new class with your desired behaviour.

Class is a blueprint for its instances... Why do you want to create an object without it's blue print...

All the runtimes restrict you to create object without a blueprint you can however use the object to get access to its blue print and this is called Reflection... An object reflects its blue print.

I think that what best describes what you pretend are anonymous classes in Java. C# although, doesn't allow you to override a method with an anonymous type, nor it allows you to override anything in fact.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top