[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Import different namespace based on user credentials

Posted on 2006-06-05
3
Medium Priority
?
277 Views
Last Modified: 2010-08-05
Hi,

Depending on what permissions a user has that logs into a system the page should dynamically import the correct namespace for those permissions. (a sort of if-statement for namespaces). Is this possible? If so, please show with code example.

thanks, Gil  
0
Comment
Question by:GoldyLock
3 Comments
 
LVL 1

Expert Comment

by:paltareq
ID: 16831294
i think sthere is no way to import namespace programaticaly ....

but what the problem if u import all namespace  and in the code give all users
different access acording to his  permissions ?
0
 
LVL 4

Expert Comment

by:Nazermohideeen
ID: 16832143
There could be a possibility by using compiler directives #if ... #else ... #endif. You will have to access the windows temprary variables, and see who is the current user. But this is a strange requirement, Can excatly explain why you have such a requirement, may be I could suggest an alternative solution.

But if you stick with your requirement, You will have to do some research on how to implement this using the directive. But I believe that this is the only way, by which  you could avoid few lines of code from compiling, based on condition.

NM
0
 
LVL 25

Accepted Solution

by:
dstanley9 earned 2000 total points
ID: 16832879
#if ... #else ... #endif  directives are evaluated at compile-time, not run-time, so they would not work.

Importing namespaces (as you may already know) are just giving you a compile-time shortcut to classes.  All namespaces are fully qualified by the compiler to ensure type-safety, so there's no way to programatically choose the namespace at run time.  

I suppose you could use refelction to query for the particular type, but IMHO that would make the code 1) hard to read and 2) even harder to debug.  Something like (pseudo-code):

if(user is admin)
  nameSpace ="Company.Project.Component.Administrator";
else
  nameSpace ="Company.Project.Component.User";

Assembly assembly = {load assembly containing types}
Object o = assembly.CreateInstance(nameSpace + ".MyObject");

However, for this to be of any use, both classes must implement a common interface.

A better method would be to have a common interface between the classes and just create the proper type at run-time:

public interface  IMyInterface
{
  string MyString {get; set;}
}

public class MyAdminClass : IMyInterface
{
  string MyString
  {
    get
    {
      return "I am an Administrator!!!";
    }
  }
}

public class MyUserClass : IMyInterface
{
  string MyString
  {
    get
    {
      return "I am just a user. :(";
    }
  }
}


public void Main()
{
  IMyInterface myObject;
  if ({user is admin})
    myObject = new MyAdminClass();
  else
    myObject = new MyUserClass();

  Console.WriteLine(myObject.MyString);
}
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

829 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