C# function | return an objected with its type, so i dont have to cast it. RRS feed

  • Question

  • User2110873642 posted

    i want a function to return an object casted to the type that a caller specifies


            public T GetCache<T>()
    return (T)Convert.ChangeType(CachedModel, typeof(T));

    Usage (as you see, it does not get casted, because it does not have any property suggestions)

    Saturday, August 1, 2020 10:39 PM


  • User2110873642 posted
        public T Foo<T>()
                return (T)anotherFoo;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 2, 2020 3:47 PM