简体   繁体   中英

How to encapsulate Method with generic type constraints

i've got a question and I do not have a clue of how to do it. Actually I'm not sure if the title is right.

I want to encapsulate a method which has generic params. The idea is to add some logging calls etc. around the method call.

Therefore this function

public void Load<T>(T spObject, params Expression<Func<T, object>>[] retrievals)
{
    try
    {
      ... .Load(spObject, retrievals);}...
    }
}

should encapsulate a function which looks like this.

public void Load<T>(T spObject, params Expression<Func<T, object>>[] retrievals) 
where T : ClientObject

The Where T : ClientObject has to be removed because it would need to reference an additional dll in another project.

When I try to remove the where i get an error saying

The type 'T' cannot be used as type parameter 'T' in the generic type or method 'Microsoft.SharePoint.Client.ClientRuntimeContext.Load(T, params ...

Hope some have an Idea how I could handle this.

Thanks :-) Greetz

This is not an encapsulation problem but an indirect reference one.

If you don' want to link to this "other" dll but you still want to use the load method, then you'll need to directly reference Microsoft.SharePoint.Client.ClientObject since that's the class that the where clause of the load method needs to use.

This is what you nee to add to your source files:

using Microsoft.SharePoint.Client;

Then you will need to add

Microsoft.SharePoint.Client.dll
Microsoft.SharePoint.Client.Runtime.dll

to your project references. The dlls should be in C:\\Program Files\\Common Files\\Microsoft Shared\\SharePoint Client or in C:\\Program Files\\Common Files\\Microsoft Shared\\web server extensions\\14\\ISAPI depending on your sharepoint version.

This should be enough to compile and link your program.

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