.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Interview Question >> .NET Framework >> Post New Question Subscribe to Interview Questions

How can you access a private method of a class?

Posted By :Jean Paul     Posted Date :September 28, 2010    Points :10   Category :.NET Framework 
Use .Net Reflection. Using reflection we can invoke private methods too.

Example:

Car car = new Car();
Type type = car.GetType();
MethodInfo methodInfo = type.GetMethod("ShiftGearUp", BindingFlags.NonPublic | BindingFlags.Instance);
methodInfo.Invoke(car, null);

Pre: Create a class Car with private method "ShiftGearUp"

You can also find related Interview Question to How can you access a private method of a class?  below: 

Can we call a base class method without creating instance?

  
Its possible If its a static method.

Its possible by inheriting from that class also.

Its possible from derived classes using base keyword. (More...)

What is Private Constructor? and it's use? Can you create instance of a class which has Private Constructor?

  
When a class declares only private instance constructors, it is not possible for classes outside the program to derive from the class or to directly create instances of it. (Except Nested classes)
Make a constructor private if:

1) You want it to be available only to the class itself. For example, you might have a special constructor used only in the implementation of your class' Clone method.

2) You do not want instances of your component to be created. For example, you may have a class containing nothing but Shared utility functions, and no instance data. Creating instances of the class would waste memory. (More...)

If any .NET language has access to everything in the Base Class Library, why is there so much talk about C#?

  
Although in theory all .NET languages have equal access to the BCL, in reality it was left up to the language teams to determine what level of support they wanted to offer, at least beyond the minimums needed for basic compliance. In our opinion, C#, because it was developed as a new language specifically for the .NET platform, has the best support for .NET applications. (More...)

How can I look up a parent class method or property of any System-level object?

  
Use the Class View window, accessed from the standard toolbar by clicking View | Class View. (More...)

Are private class-level variables inherited?

  
Yes, but they are not accessible, so looking at it you can honestly say that they are not inherited. But they are. (More...)

Are private class-level variables inherited?

  
Yes, but they are not accessible, so looking at it you can honestly say that they are not inherited. But they are. (More...)

Can you allow class to be inherited, but prevent the method from being over-ridden?

  
Yes, just leave the class public and make the method sealed. (More...)

Which method is used to serialize an object into an XML document from Xmlserializer class?

  
The "Serialize" method is the first step of serialize process and used to create an XML document (More...)

If a method is marked as protected internal who can access it?

  
Access is limited to the current assembly or types derived from the containing class (More...)

Whether we can create a private constructor for a class?

  
Yes it is possible. we can create a private constructor for a class (More...)

Explain about private and internal Access Modifiers?

  
Private:
========
private is an access modifier for type members only. private access is the least accessible and such members are only accessible within the body of the containing type.
Note that nested types within the same containing body can also access those private members.

Internal:
--------
internal is an access modifier for types and type members. internal members are accessible only within file of the same assembly. It is an error to reference an internal type or internal type member outside the assembly within which it was declared.

A common use of internal access is in component-based development because it enables a group of components to interact in a private matter without being exposed to the outer world. For example, a Data Access Layer could have several classes with internal members that are only used by the DAL.
(More...)

Quick Links For Interview Questions Categories:
ASP.Net Windows Application   .NET Framework   C#   VB.Net   ADO.Net  
Sql Server   SharePoint   Silverlight   OOPs   JQuery   JavaScript/VBScript
Biztalk Patten/Practices .IIS WCF WPF WWF
Networking Aptitude Others   All      

Find questions, FAQ's and their answers related to .NET, C#, Vb.Net, Sql Server and many more.

 
Now you can find lots of .NET, C#, Vb.Net, SQL Server,Windows, ASP.Net related Questions and their Answers here at www.dotnetspark.com. Our aim is to help you pass your certification Exams (MCP, MCSD, MCAD etc.,) with flying scores and get good name in your company.

So, Start looking our Interview Question section daily and improve your .NET Skills. You can also help others by posting Interview Questions and their Answers in this section.


Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend