-1

I'm using ServiceStack(4.0.46.0) and would like to add fields to ServiceStack.AuthUserSession. Creating a new class using Inheritance is a solution but I want the new class to still be of type ServiceStack.AuthUserSession

For example

    Public Class MyUserSession : ServiceStack.AuthUserSession
    {
         Public string NewFieldOne {get; set;}
         Public string NewFieldTwo {get; set;}

    }

....but MyUserSession would still be of type ServiceStack.AuthUserSession. Is that possible? This is on Framework 4.5.

Jason R.
  • 379
  • 1
  • 6
  • 19
  • 2
    If you are asking if it is possible to add fields to a base type and have it appear in the base type the answer is no. However you can cast to the derived type if it truely is an instance of the derived type and use the fields... – Ron Beyer Nov 19 '15 at 20:24
  • Not marking this as a dupe, but you can do similar stuff with a [`ConditionalWeakTable`](http://stackoverflow.com/a/31480983/3764814). The syntax will be different though, you'll have to use some extension methods. – Lucas Trzesniewski Nov 19 '15 at 20:25
  • 2
    _"MyUserSession would still be of type `ServiceStack.AuthUserSession`"_ -- if class `B` inherits class `A`, it _is_ a class `A` _as well_ as a class `B`. This is a fundamental truism of OOP, and the whole point of inheritance. Your `MyUserSession` type can still be used anywhere that a `ServiceStack.AuthUserSession` type is required, because it _is_ one of those too. Taken literally, your question is self-answering; so if that's _not_ what you meant to ask, please edit your question so that it's clear what you _did_ mean to ask. – Peter Duniho Nov 19 '15 at 20:28
  • I understand @PeterDuniho, I thought if class B inherits from class A, class B was still considered a different class. If you put this as a answer, I will mark it correct for you. – Jason R. Nov 19 '15 at 20:55
  • You might be able to fake it using extension methods, but they would have to be treated as methods, not properties. – Bradley Uffner Nov 19 '15 at 21:01
  • _" I thought if class B inherits from class A, class B was still considered a different class"_ -- "considered" by whom? It obviously is a different class in some sense of the word "different". For example, to create an instance of it, you need `new B()` and not `new A()`. On the other hand, while it's a different type from `A`, it still _is_ an `A`. Just like the words "man" and "woman" are different from the word "human", even as at the same time, both "man" and "woman" _are_ in fact "human". – Peter Duniho Nov 19 '15 at 21:09
  • I can't in good conscience post an answer to your question without knowing for sure that I actually understand the question. As stated, you seem to be asking about a basic OOP truism, but most questions arise from some genuine real-life problem you are trying to solve. If you could add detail to your question that shows in what way using the class just as you've shown here _doesn't_ work, and explain precisely in what way it doesn't work, then I or someone else can provide a truly good answer. – Peter Duniho Nov 19 '15 at 21:10

1 Answers1

0

Have a look at different ways you can extend ServiceStack Authentication

Creating a custom type that inherits AuthUserSession is one option. You can tell ServiceStack to use your Custom UserSession in the AuthFeature constructor:

Plugins.Add(new AuthFeature(() => new MyUserSession(), ...);
Community
  • 1
  • 1
mythz
  • 141,670
  • 29
  • 246
  • 390