3 Replies - 268 Views - Last Post: 29 October 2019 - 07:23 AM Rate Topic: -----

#1 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6823
  • View blog
  • Posts: 28,274
  • Joined: 12-December 12

get type for generic specifier

Posted 29 October 2019 - 03:00 AM

I have a tonne of statements like the following

var CanOnlySeeOwnJobs = new GenericSqlParameter<string>("CanOnlySeeOwnJobs", this.CanOnlySeeOwnJobs);


Initially I have copied and edited all these lines to get all the parameters I need, with the default <string>. But CanOnlySeeOwnJobs is a bool, so I need to edit the lines for any non-string parameters.

Is it possible to get the type for < > directly from the variable? I tried and failed with

var CanOnlySeeOwnJobs = new GenericSqlParameter<this.CanOnlySeeOwnJobs.GetType()>("CanOnlySeeOwnJobs", this.CanOnlySeeOwnJobs);


Quote

Operator > cannot be applied to operands of type Type and string.


Is This A Good Question/Topic? 0
  • +

Replies To: get type for generic specifier

#2 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7501
  • View blog
  • Posts: 15,544
  • Joined: 16-October 07

Re: get type for generic specifier

Posted 29 October 2019 - 04:36 AM

Without seeing GenericSqlParameter I can only guess. It should infer it, though:
void Foo<T>(T x) {
    var t1 = x == null ? "null" : x.GetType().Name;
    var t2 = typeof(T).Name;
    Debug.WriteLine($"value={x}, type={t1}, T={t2}");
}

Foo("bar");
Foo(42);
// Foo(null); // this won't work
Foo<double?>(null);
object x = "baz";
Foo(x);



Results:
value=bar, type=String, T=String
value=42, type=Int32, T=Int32
value=, type=null, T=Nullable`1
value=baz, type=String, T=Object


So, if the thing you're passing is an object, you need some special allowance for that.
Was This Post Helpful? 1
  • +
  • -

#3 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6823
  • View blog
  • Posts: 28,274
  • Joined: 12-December 12

Re: get type for generic specifier

Posted 29 October 2019 - 05:33 AM

Thank you @baavgai.

I could post the relevant code if you'd like, but, on glancing through it, I think it is providing the parameter's value as an object.

I think I'll leave it for the moment, though, and just provide the type for each parameter.
Was This Post Helpful? 0
  • +
  • -

#4 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7501
  • View blog
  • Posts: 15,544
  • Joined: 16-October 07

Re: get type for generic specifier

Posted 29 October 2019 - 07:23 AM

You know, there's nothing stopping you from instantiating this with a function. e.g.
class GenericSqlParameter<T>{
    public string Name { get; }
    public T Value { get; }
    public GenericSqlParameter(string name, T value) {
        Name = name;
        Value = value;
    }
    public override string ToString() => 
        $"{Name}={Value}, type={((Value == null) ? "null" : Value.GetType().Name)}, T={typeof(T).Name}";
}

static GenericSqlParameter<T> CreateParam<T>(string name, T x) =>
    new GenericSqlParameter<T>(name, x);

static GenericSqlParameter<string> CreateParam(string name, string x) =>
    new GenericSqlParameter<string>(name, x);

static void TestFoo() {
    Debug.WriteLine(CreateParam("foo", "foo"));
    Debug.WriteLine(CreateParam("bar", 42));
}



Hmm, the 42 also worked with that. Looks like classes are less forgiving.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1