Dynamic Property Name in Class/Interface?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Dynamic Property Name in Class/Interface?

  1. #1
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    26

    Dynamic Property Name in Class/Interface?

    Hey.

    Is there any way that I can "define" the PropertyName in a class at runtime/compiling? Either by through the constructor add the PropertyName or something else.

    An example.

    Say I have a class that should contain two Properties but I don't want to name them. Much like <T>, I want it to be used with different types - all I know is there should be two Fields.

    Code Example
    Code:
    interface DynamicProperty<T>
    {
    
         T (DynamicName_PlaceHolder);
         T (DynamicName_PlaceHolder);
    
    }
    And to get the the data from that property without knowing it's name, is by searching the object/class for all its properties. I have an interface, an abstract class that inherits its, and other classes that inherits it. BUT, the BaseClass has a method that shows all the PropertyValues of the class as long as it's not part of the BaseClass.

    So, what I'm trying to say is:

    Interface -> Base Class (With method for getting PropertyName & PropertyValue of object that is not part of Base Class) -> Child Class

    so if I:
    Base Class.GetAllProperties(); // returns null
    but if I have a Property named "Name" in Child Class:
    Child Class.GetAllProperties(); // returns "Name : My Name"

    so, this way when I build a system that has Properties that isn't know at the time, it works. Therefor I would like to know if there's any way to implement this?

    Create a property at runtime or anything like that.

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Dynamic Property Name in Class/Interface?

    No, there's not as it would make little sense in a language like C#. What benefits would an interface provide if none of the methods/properties were known at compile time anyway? None. What you are asking for does not make sense, but it is simply a case of trying to solve a problem in the wrong way and not being able to figure out that last 5&#37; that makes no sense.

    So, instead of asking us to solve that last 5% that makes no sense, tell us what problem you are actually trying to solve with this design. Sure, you could hack something together with reflection or dynamic, but I would wager that it is not needed at all and you just need to modify your design approach.
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

  3. #3
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    26

    Re: Dynamic Property Name in Class/Interface?

    OK. If that's not possible.

    I have an interface, and a abstract base class that implements it. Then, from that base class a few other new classes are created (Different groups within this class), then from these classes (these groups) the actual objects are created from. So, I create Object A that inherits (Interface-> Base Class -> Group A), and I create Object A (Interface-> Base Class -> Group B). The only thing they have in common is the base class, so to speak. So if I want to check which objects have a certain attribute in common with an object from Group A, B, C, D or any other group for that matter. I don't want the common attribute to be "Base Class" because that would mean every class would be included.

    Therefor, I though because there would be something that I add to each of these objects (or groups) that makes them common in a certain way with another group of classes or other objects without having to refer to the "top" class.

    What is the best way to do this? It's suppose to be some kind of attribute, doesn't necessarily need to have a value. Just a way to get different types of classes that really doesn't have anything in common do have something in common.

    An example:
    Code:
    Interface Animal
    {
    }
    
    Abstract class Animal
    {
    }
    
    Animal : Mammal
    {
    }
    
    Animal : Reptile
    {
    }
    
    Dog : Mammal
    {
    }
    
    Snake : Reptile
    {
    
    }
    
    Here, Snake and Dog have nothing in common but they're both animals. If I add an attribute in someway (Interface? Or an abstract class? I reckon a Property is not a good choice here)
    This way, I thought OK they all have the same "Attribute" but the Attribute could be defined in different ways (which is why I posted this, because it was what I wanted). I wanted a base Attribute, but the Dog class might want to add a few extra Properties to it and still be able to have something in common with the Snake which it would otherwise not have.

    Any suggestions on what is recommended for this?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center