Solved

How to Access Protected methods in C#

Posted on 2012-03-19
5
388 Views
Last Modified: 2012-03-19
I was under the impression that it was plausible to accessing protected methods by deriving a new class by the class with the protected method..however, I am trying to use a method to add users with a certain GUID in C# and I am unable to access the GrantAccess methods that I am showing a link for below.  

http://technet.microsoft.com/en-us/library/microsoft.sharepoint.administration.spdatabase.grantaccess.aspx

So, how do I get access to the grantaccess methods in C#?  Thanks!
0
Comment
Question by:VBBRett
  • 3
  • 2
5 Comments
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 500 total points
ID: 37738801
these are protected internal methods.
To Access these methods, you need to derive from SPDatabase class and then from your derived class you will be able to access these methods.

you can always write a public method in your derived class which allows user to add GUID as user, this method will internally use the protected grantacess method as given below:-
public sealed class MyDerivedDatabase : SPDatabase
{
public void AddUser(GUID userId)
{
this.GrantAccess(new SecurityIdentifier(userId.ToString());
}
}

MyDerivedDatabase db=new MyDerivedDatabase();
db.AddUser(GUID.NewGuid());

Open in new window

0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 37738893
To Access these methods, you need to derive from SPDatabase class and then from your derived class you will be able to access these methods.
Incorrect. The methods are marked internal, which means only other types from the same assembly can gain access to that method. You might be able to use Reflection, but the methods were marked internal for a reason (meaning someone didn't want outside code using them), so it may not be worthwhile fiddling with these methods.

What is your goal?
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 37738898
P.S.

I agree with the explanation of protected. It's the internal that's giving you the grief  = )
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37738985
Hi @kaufmed,

i though i made a mess here!!!
Just Rechecking and found that Methods are marked as protected internal....

so they can be used by derived type as well as the types in the same assembly.

Keep Well...
Happy Coding -:)
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 37739017
The mistake is mine--due to my misunderstanding of what protected internal entails. I took them as a combined meaning--can only be used within the same assembly, and only if derived--whereas it seems their meaning is separate--can be used within the same assembly or derived in an external assembly. I learned something new. Yay  = )
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now