0

enter image description hereI need to expose my generic class property directly. SEE IN CODE BELOW

namespace ConsoleApp2
{
    public class KTEST<T>
    {
        public int PageNumber { get; set; }
        public int PageSize { get; set; }
        public T Filter { get; set; }

    }

    public class Request
    {
        public int CountryId { get; set; }
    }

    class Program
    {
        public static void Main(string[] args)
        {
            KTEST<Request> kTESTRequest = new KTEST<Request>();

            kTESTRequest.PageNumber = 1;
            kTESTRequest.PageSize = 20;

            kTESTRequest.Filter.CountryId = 1;
        }
    }
}

Now, how can I get the countryId property without use of 'Filter'. I need to use kTESTRequest.CountryId instead of kTESTRequest.Filter.CountryId

Could you please help me to out this.

Bunty Choudhary
  • 185
  • 1
  • 3
  • 11
  • It's impossible – Backs Mar 27 '20 at 09:19
  • Unless you have an interface you cant, why do you really need to do this? – TheGeneral Mar 27 '20 at 09:19
  • @MichaelRandall how to implement that thing with interface. Could you please explain little more – Bunty Choudhary Mar 27 '20 at 09:20
  • @MichaelRandall I just edit the question and add swagger ui for further reference. Its for API request purpose.It looks some thing not good. It should be only CountryId. As well request for api should be only "http://localhost:0000/api/xxx?CountryId=1". But currently it is http://localhost:0000/api/xxx?Filter.CountryId=1 – Bunty Choudhary Mar 27 '20 at 09:28
  • @Bunty That seems unrelated to the question you started with. – ProgrammingLlama Mar 27 '20 at 09:30
  • I can post an answer to this, but first please explain exactly why you need to access data from within the filter object. I'm looking for a top level explanation, e.g. "because filters should output a SQL query and I want to retrieve that SQL string". The context will help in making an answer more concrete and understandable instead of abstract. To help you focus: try to explain to me what you expect **any** filter object to contain - not just what a Request contains. – Flater Mar 27 '20 at 09:30
  • @Flater we have PageNumber, pageSize and filter. Where I think first two's parameter are understandable already. Now T could be any class which have N number of property having any type of datatype. – Bunty Choudhary Mar 27 '20 at 09:39
  • 1
    @BuntyChoudhary: "it can be anything" is about as vague as you could possible ever be. Your current expectation is contradictory. On the one hand, you tell me that "it could contain anything". On the other hand, your code is telling me that the filter definitely has a `CountryId` property. It's one or the other. This is either an XY problem or a deep misunderstanding of what generics are. I'm still writing an answer, but in absence of concrete information from your side it's going to be a theoretical one. – Flater Mar 27 '20 at 09:41
  • @Flater Suppose I have another class also which is like this public class Request2 { public int CountryId { get; set; } public int StateId { get; set; } public int CityId { get; set; } } Now how we can proceed? – Bunty Choudhary Mar 27 '20 at 09:43
  • 1
    @BuntyChoudhary: Okay, so that second class also has a CountryId. Will every filter have a country id? Will you expect to use the StateId from your filter too? What happens to the code when you use the original request class and try to access the StateId, which does not exist on that original request class? You really need to think through what you want to happen in those cases, before you're able to write the code that can handle all these cases. – Flater Mar 27 '20 at 09:45
  • I really didn't get you. But one thing is sure that not every filter request have countryId. – Bunty Choudhary Mar 27 '20 at 09:47
  • @BuntyChoudhary: If not every filter will have a country id, then it doesn't make sense to expect to access the country id of your filter (since it doesn't always have one - what would happen when you access something that doesn't exist?). That is a logical inevitability. You need to explore why you need to access these properties, what they mean to the filter (regardless of the concrete type used), and how you expect them to be handled. We can't tell you what it is that _you_ want to have happen. – Flater Mar 27 '20 at 09:53
  • Does this answer your question? [Generic Property in C#](https://stackoverflow.com/questions/2587236/generic-property-in-c-sharp) – Pavel Anikhouski Mar 27 '20 at 10:08
  • @PavelAnikhouski no – Bunty Choudhary Mar 27 '20 at 10:13
  • 1
    Why should KTEST be generic and why can't you put all filters as properites in KTEST class? – Sergey94 Mar 27 '20 at 10:43
  • Why are you using generics here? You're very insistent on both using generics and doing non-generic things. – ProgrammingLlama Mar 27 '20 at 13:14

1 Answers1

1

The only way to do this is with an interface

Given

public interface IMyInterface
{
    public int CountryId{ get; set; }
}

Usage

public class KTEST<T>  where T : IMyInterface
{
    public int PageNumber { get; set; }
    public int PageSize { get; set; }
    public T Filter { get; set; }
    public int CountryId => Filter.CountryId
}

Requirement

public class Request : IMyInterface
{
    public int CountryId { get; set; }
}
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • But if you have another request like public class Request1 { public int CountryId { get; set; } public int StateId { get; set; } public int CityId { get; set; } } How would we can handle it. – Bunty Choudhary Mar 27 '20 at 09:36
  • @BuntyChoudhary add the interface, if you cant, then you cant do what you want to – TheGeneral Mar 27 '20 at 09:37
  • I can't add multiple interface, I need to make KTEST as a generic class – Bunty Choudhary Mar 27 '20 at 09:41
  • 1
    Pedantically, an interface isn't the _only_ way. A base class works just as well. It doesn't really change the answer but it does somewhat reframe the expectations on how you can implement this. – Flater Mar 27 '20 at 09:43