简体   繁体   中英

Generic Class:: Type Parameter <T> Property Inheritance

My code:

class ClassX;
class Class2 : ClassX;
class Class3 : ClassX;

Class2 and Class3 have no association to each other whatsoever, EXCEPT for them sharing the same base class. Class2 and Class3 don't share the same properties, data types or amount of properties.

Now, what I want to be able to do is:

class Class<T> : T;

static void Main()
{
    Class<Class2> foo = new Class<Class2>();
    foo.Class2_Property = value;

    Console.Read();
}

I'm aware this code is not possible, but is there any work around?

In C++, you can use an operator overload to do this like so:

public : operator return_data_type const;    

Thanks in advance.

PS: I already opened a Question about this right here , but I wasn't certain how to ask my question, but I am now. I left it open in-case somebody finds an answer to their question through the answers I've gotten.

EDIT #1:

I've got a Packet class that has the shared Packet properties and methods, and different packets have their own header and different content. I'm changing my packet system. Basically I want to do Packet where PacketClass is the name of the specific Packet, for example LoginInfo. LoginInfo class will contain properties:

public PacketHeader Header;
public string Username;
public string Password;

Therefore:

    Packet<LoginInfo> foo = new Packet<LoginInfo>();
    foo.Username = "user";
    byte[] buf = foo.ToArray(); // Part of the Packet<T> class

That's what I meant with that each class has it's own properties. I hope that gives you got a good idea of what I'm trying to do.

You probably want to completely rethink your implementation, as this isn't what C# is designed to do. You can do hacky things to make this work, but it's not very pretty:

class Class<T> where T : new()
{
    public T Value = new T();
}

This creates a 'container' for your 'base' type, relying on a constraint that the type T must have a parameterless constructor.

Then you can do:

class ClassX {}

class Class2 : ClassX 
{ 
    public int test = 10;
}

Then you can consume your fake 'derived' class like this:

Class<Class2> foo = new Class<Class2>();
var test = foo.Value.test;

You also have to make any fields you're interested in on Class2 public for this to work.

You can't do exactly what you want using C#, but you shouldn't.
Here's design issue. The packet is not a packet content. It contains some header and some content:

interface IPacketItem
{
    byte[] ToArray();
}

class Packet<THeader, TContent>
    where THeader : IPacketItem
    where TContent : IPacketItem
{
    // header and content must be initialized somehow;
    // maybe, using existing instances, maybe using new() constraint
    public THeader Header { get; private set; }
    public TContent Content { get; private set; }

    public byte[] ToArray()
    {
        // builds an array using IPacketItem.ToArray
    }
}

var loginPacket = new Packet<PacketHeader, LoginInfo>();
loginPacket.Content.UserName = "user";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM