views:

282

answers:

8

Hi, 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.

+3  A: 

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.

Dave Markle
+2  A: 

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...

Calanus
A: 

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

Blair Conrad
A: 

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.

S M Kamran
+1  A: 

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

thecoop
+1  A: 

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.

lc
+10  A: 

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.

Jon Skeet
LOL Jon, be careful how good your answer is. Answering the question like this is like giving a double-barreled shotgun to a newbie for foot-shooting! ;-)
Dave Markle
I wonder if this could become possible to do with the new dynamic runtime though.
DrJokepu
@DrJokepu: You'd do much the same thing with the dynamic runtime. In fact, ExpandoObject lets you do exactly this, but dynamically - you can assign a delegate to a property, then execute it elsewhere.
Jon Skeet
A: 

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.

bruno conde