Ok, I have the following structure. Basically a plugin architecture
// assembly 1 - Base Class which contains the contract
public class BaseEntity {
public string MyName() {
// figure out the name of the deriving class
// perhaps via reflection
}
}
// assembly 2 - contains plugins based on the Base Class
public class BlueEntity : BaseEntity {}
public class YellowEntity : BaseEntity {}
public class GreenEntity : BaseEntity {}
// main console app
List<BaseEntity> plugins = Factory.GetMePluginList();
foreach (BaseEntity be in plugins) {
Console.WriteLine(be.MyName);
}
I'd like the statement
be.MyName
to tell me whether the object is BlueEntity, YellowEntity or GreenEntity. The important thing is that the MyName property should be in the base class, because I don't want to reimplement the property in every plugin.
Is this possible in C#?
I think you can do it through GetType:
public class BaseEntity {
public string MyName() {
return this.GetType().Name
}
}
public class BaseEntity {
public string MyName() {
return this.GetType().Name;
}
}
"this" will point to the derived class, so if you were to do:
BaseEntity.MyName
"BaseEntity"
BlueEntitiy.MyName
"BlueEntity"
EDIT: Doh, Gorky beat me to it.
C# implemented a way to look at objects called Reflection. This can return information about the object you are using.
The GetType() function returns the name of the class you are calling it on. You can use it like this:
return MyObject.GetType().Name;
Reflection can do a lot of things. If there is more that you want to know about reflection you can read about it on these websites:
MSDN Reflection Article
Oreilly Chapter
Code Source Tutorial
Change your foreach statement to the following
foreach (BaseEntity be in plugins) {
Console.WriteLine(be.GetType().Name);
}
If you haven't overridden the ToString() method for the class, then you can just write the following
string s = ToString().Split(',')[0]; // to get fully qualified class name... or,
s = s.Substring(s.LastIndexOf(".")+1); // to get just the actual class name itself
using yr code:
// assembly 1 - Base Class which contains the contractpublic class BaseEntity
{
public virtual string MyName // I changed to a property
{
get { return MyFullyQualifiedName.Substring(
MyFullyQualifiedName.LastIndexOf(".")+1); }
}
public virtual string MyFullyQualifiedName // I changed to a property
{
get { return ToString().Split(',')[0]; }
}
}
// assembly 2 - contains plugins based on the Base Class
public class BlueEntity : BaseEntity {}
public class YellowEntity : BaseEntity {}
public class GreenEntity : BaseEntity {}
// main console app
List<BaseEntity> plugins = Factory.GetMePluginList();
foreach (BaseEntity be in plugins)
{ Console.WriteLine(be.MyName);}
Try this pattern
class BaseEntity {
private readonly m_name as string;
public Name { get { return m_name; } }
protected BaseEntity(name as string) {
m_name = name;
}
}
class BlueEntity : BaseEntity {
public BlueEntity() : base(typeof(BlueEntity).Name) {}
}
Related
I have scenario like below
public abstract class Test
{
public string name;
public abstract bool is_selected();
}
public class Campus : Test
{
}
Now Campus must and should implement is_selected() method otherwise it throws an error.
Adding to the same lines I want 'name' field also like that. I mean name field must be given a value.
How can I do that?
Any help greatly appreciated.
Thanks in advance :)
I can find two options
Use a parameterized constructor, so the derived class must provide a constructor which initialize the fields.
public abstract class Test
{
public string name;
protected Test(string name){ this.name = name; }
}
public class Campus : Test
{
public Campus() : base("Init the name here") {}
}
Use an abstract property, this is already metioned by other people.
public abstract class Test
{
public abstract string name { get; }
}
public class Campus : Test
{
public override string name => "name";
}
I have many classes that have the following members/methods:
private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public bool isNamed(String name) { return getName().Equals(name); }
Every time I create a new class that has a member "name", I have to rewrite all these.
Is there a way to write the methods one time and to make them apply to any class I want?
Your code can be converted to:
public String Name { get;set;}
Then you can use it as so:
nObject.Name = "Stefan";
if(nObject.Name == "Stefan"){
// do something
}else{
// do something else
}
To apply to all the classes automatically you can just make this into an interface:
public interface INameable{
public String Name {get;set;}
}
Doing this will allow you to inherit from other base classes of importance.
see here for an example
class YourClass : INameable{
//implementation
}
And now, YourClass has "Name" property automatically inserted.
You'd simply define a base class (you could make it abstract):
public abstract class Named
{
public string Name { get; set; }
}
and inherit from it:
public class Person : Named
{
}
You don't really need isNamed as in C#, it is perfectly safe to compare strings with ==.
If your class already inherits from another class which is not Named, you'll have to manually add the Name auto property or resort to simulated multiple inheritance.
Alternatively, you could create a specific modification of Named for every base class:
public abstract class NamedLifeForm : LifeForm
{
public string Name { get; set; }
}
public class Person : NamedLifeForm
{
// Person inherits both a Name and all relevant members of LifeForm
}
Another alternative would be to create a generic wrapper, Named<T>, that would have two properties: the Name and an instance of T. But that would make construction and access cumbersome, so I don't recommend it.
C# has AutoProperties just for that:
public String Name {get; set; }
This handles both the getName() and the setName() you talked about.
Usage:
To set a value: Name = "MyName;
To get a value: string theName = Name;
I'd suggest reading up on Object Oriented Programming. You can save yourself a lot of time and effort (and heckling). Here is a good primer http://www.amazon.com/Beginning-Object-Oriented-Programming-Dan-Clark/dp/1430235306
To answer your specific question, you should read about inheritance. It lets you define a "Parent" class with functions. Then you can inherit with "Child" classes and have those same functions.
http://msdn.microsoft.com/en-us/library/ms173149(v=vs.80).aspx
Here is a code example
public class PersonBase
{
private String name;
public String getName()
{
return this.name;
}
public void setName(string name)
{
this.name = name;
}
public bool isNamed(string name)
{
return this.name.Equals(name);
}
}
public class Employee : PersonBase
{
}
Employee will now have whatever was defined by PersonBase.
As others have pointed out, you can simplify you code with properties. Also you should check for null values before using "this.name".
Here is a link to what properties are:
http://msdn.microsoft.com/en-us/library/x9fsa0sw(v=vs.80).aspx
The simplified code example would be:
public class PersonBase
{
public String Name { get; set; }
}
public class Employee : PersonBase
{
}
I hope this helps get you pointed in the right direction for learning about these concepts.
I have an interface for a base class, and every class that inherits from the base class should have an identifying field which tells the application what kind of object it is.
I wanted to use this property in two different ways:
Without creating an instance of the object
if (someValue == TestA.Id)
return new TestA();
elseif (someValue == TestB.Id)
return new TestB();
And as a property of the interface
void DoSomething(ITest testObject)
{
SomeValue = testObject.Id;
}
Is there an easy way to define the Id field in the interface, but still have it available to use without creating an instance of the class?
Right now I am using the following code. I could add a read-only Id property to the interface which returns the const string, however I was hoping there was a simpler way that I'm just not aware of.
public interface ITest
{
}
public class TestA : ITest
{
public const string Id = "A";
}
In short - no.
In order to be able to do this, you'd need to be able to specify this as a instance property on the interface (and implement it in the instance), and as a static property on the type.
The compiler won't let you do this.
You can put it in the interface, and also have it as a static property. Something like:
interface IInterface { Id { get; } }
class Class : IInterface
{
public static Id { get { return 1; } }
public Id { get { return Class.Id; } }
}
I've faced a similar problem, Rachel, and I've always (unfortunately) resorted to having that factory code rely on reflection to get a "TypeID" public static property on each concrete type... thus making an additional aspect of the contractual interface, but not having it in the C# interface code.
You could do it this way.
public interface ITest
{
SomeValue Id{ get;}
}
public class TestA : ITest
{
public SomeValue Id
{
get {return TestA.StaicId; }
}
public static SomeValue StaticId
{
get {return "This is TestA";}
}
}
if (someValue == TestA.StaticId)
return new TestA();
How about using attributes? Here's a small example of what can be done:
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
public class IdAttribute : Attribute
{
public IdAttribute(string id)
{
this.Id = id;
}
public string Id { get; set; }
}
public interface IMyInterface
{
}
public abstract class BaseClass : IMyInterface
{
public static string GetId<T>() where T : IMyInterface
{
return ((IdAttribute)typeof(T).GetCustomAttributes(typeof(IdAttribute), true)[0]).Id;
}
}
[Id("A")]
public class ImplA : BaseClass
{
}
[Id("B")]
public class ImplB : BaseClass
{
}
internal class Program
{
private static void Main(string[] args)
{
var val1 = BaseClass.GetId<ImplA>();
var val2 = BaseClass.GetId<ImplB>();
Console.ReadKey();
}
}
it seems if i use an custom class as base of an entity,the ObjectContext.CreateObjectSet will fail with stackoverflow exception
code is:
// This is generated by EF4 and i modify it to my custom class
public partial class EntityA : GClass<EntityA>
{
......
}
public partial class TestEntities : ObjectContext
{
public ObjectSet<EntityA> EntityAs
{
get
{
if ((_EntityAs == null))
{
// here will throw stackoverflow exception
_EntityAs = base.CreateObjectSet<EntityA>("EntityAs");
}
return _EntityAs;
}
}
private ObjectSet<EntityA> _EntityAs;
}
// This is custom class
public partial class EntityA
{
}
// This is my custom base class
public class GClass<T> : EntityObject where T : class
{
public virtual string GetStr()
{
return "GClass";
}
}
I recommend creating an interface for your entity objects instead of changing the base class. Generated code should not be modified.
Update: Due to unexplained downvotes, I'm adding the code below, which spells out precisely what I mean:
// Generated by EF4
public partial class EntityA : EntityObject
{
...
}
// Interface defined in another file
public interface IGClass<T> where T : IGClass<T>
{
string GetStr();
}
// Automatically generated by T4 template
public partial class EntityA : IGClass<EntityA>
{
public virtual string GetStr()
{
return "GClass";
}
}
The resulting code does use CRGP, but does so via an interface instead of a base class.
More info on T4 templates is here.
Are there any frameworks that assist me with this: (thinking that perhaps StructureMap can help me)
Whenever I create a new instance of "MyClass" or any other class that inherits from IMyInterface I want all properties decorated with [MyPropertyAttribute] to be populated with values from a database or some other data storage using the property Name in the attribute.
public class MyClass : IMyInterface
{
[MyPropertyAttribute("foo")]
public string Foo { get; set; }
}
[AttributeUsage(AttributeTargets.Property)]
public sealed class MyPropertyAttribute : System.Attribute
{
public string Name
{
get;
private set;
}
public MyPropertyAttribute(string name)
{
Name = name;
}
}
Use an abstract class instead (or a factory pattern if you insist on an interface).
With an abstract class, you can just do the necessary population in the default constructor with a little bit of reflection.
Something like:
abstract class Base
{
protected Base()
{
var actualtype = GetType();
foreach (var pi in actualtype.GetProperties())
{
foreach (var attr in pi.GetCustomAttributes(
typeof(MyPropertyAttribute), false))
{
var data = GetData(attr.Name); // get data
pi.SetValue(this, data, null);
}
}
}
}
Disclaimer: Code may not compile, I just wrote it from the top of my head.
Check the following frameworks from codeplex:
http://www.codeplex.com/AutoMapper for mapping and
http://fasterflect.codeplex.com/ for fast reflection to gather your properties and setvalues or getvalues.