Can I create interfaces or abstract classes in C # that declare methods with a parameter of an unknown type?

I am parsing an HTTP GET request string in my components. When trying to make it modular (the number and types of parameters can vary greatly), I want to have a base class or an abstract parameter interface that determines whether a property was set or not, as well as a Set method that sets the cost. Is there a way to do this with a variable parameter type for the Set method?

The general idea is this:

public abstract class Parameter
{
    public bool IsSet { get; protected set; }
    protected Parameter() { IsSet = false; }
    public abstract void Set( --unknown type here-- );
}

A sample child parameter would then look like this:

public class IntParameter : Parameter
{
    public int Value { get; protected set; }
    public void Set(int value)
    {
        Value = value;
        IsSet = true;
    }
}

, , . IsSet , , "" , , . , .

, , , , , .

- , . , , Google.

, , :

  • . , , , , , .
  • , of() - , , .

?:)

+3
1

, -

public abstract class Parameter<T>
{ 
    public bool IsSet { get; protected set; } 
    protected Parameter() { IsSet = false; } 
    public abstract void Set(T value);
    public T Value;
}
public class IntParameter : Parameter<int>
{
    public override void Set(int value)
    {
        Value = value;
        IsSet = true;
    }
}

# Generics

+10

Source: https://habr.com/ru/post/1733882/


All Articles