I have a class named NIFERepository
. It contains a SaveObject
method, which returns nothing and performs an operation on a database.
public class NIFERepository<E>
{
protected void SaveObject(object saveObject)
{
if (saveObject is E)
this.RepositorySession.Merge(saveObject);
}
}
1 . I wish to create a public extension method who calls this SaveObject()
method, like this:
public static class NIFERepositoryExtensions
{
public static T Save<T, E>(this T self, E saveObject) where T : NIFERepository<E>
{
self.SaveObject(saveObject);
return self;
}
}
But the protected scope doesn't allow my extensions method to recognize it.
Is there a way for me to get a method of this form to work?
2 . I created my extension method with 2 types: T
and E
. T
is the instance who called it, and E
is the defined type into my ProductRepository<Product>
, for example. When I call this method, the defined type is not shown.
Is there a way for me to get this to work?
protected
for a reason; you're probably doing something wrong.Protected method are visible inside inherited classes. So do inheritance and create a public method that calls the base protected method like
public class BaseClass
{
protected void SomeMethod()
{
}
}
public class ChildClass:BaseClass
{
public void SomeMethodPublic()
{
base.SomeMethod()
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With