简体   繁体   中英

must be a non-abstract type with a public parameterless constructor in redis

When I save an object, I get the following error :

must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type or method 'ServiceStack.Redis.RedisClient.Store<T>(T)


RedisClass.GetInstance().Store(msg); // Error here
RedisClass.GetInstance().Save();

As this is a third party's class, I can't edit it. How do I save this object?

Could you create a wrapper around the third party object to call its constructor, then store the wrapper?

Eg

public class MyWrapper
{
    public ThirdPartyObject ThirdPartyInstance { get; set; }

    public MyWrapper()
    {
        ThirdPartyInstance = new ThirdPartyObject("Constructors");
    }
}

The error is caused by that IBasicPersistenceProvider.Store<T>() has the new() generic constraint. Instead, try using IBasicPersistenceProvider<T>.Store():

RedisClass.GetInstance().As<ThirdPartyClass>().Store(msg);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM