Using Activator.CreateInstance ()

Status
Not open for further replies.

TyphoidMary

Wise, Aged Ars Veteran
154
Hey guys, I'm a senior IT student working an internship, and I am tasked with building an AD linked single sign on authentication module for our site. The short version is that the user is logged onto out network and authenticated to AD, I pull their username, pass it to get-adprincipalgroupmembership, grab the groups, and make a decision as to what areas of the site the user has access to based on their group membership. I have a user class that I want to instantiate a new object for with the username as the object reference. Thus enter the Activator.CreateInstance() method. I'll put up some pseudo-code for you, I can't put actual code up until Tuesday, I can't access our TFS from this side of the firewall.

Code:
String userName = userNameGetter();   //gets the username and stores it in a string

Activator.CreateInstance(UserClass, userName);   //this is where it all falls apart. I'm passing it a type and a string, 
                                                 //but it balks at it and says it isn't the correct overload.

...  //some business logic, calls valuesetters based on group membership.
 

William Munny

Ars Scholae Palatinae
1,409
What type of variable is "UserClass"? Is it a type, obtained with Type.GetType("Assembly.ClassName") or typeof()?

Make sure you're using the correct overload of Activator.CreateInstance - there are quite a few. Assuming "UserClass" is a type obtained with Type.GetType or similar, try using:

Code:
Activator.CreateInstance(UserClass, new object[] {userName});

In addition, are you binding to the correct version of the assembly? Use fuslogvw to check (run it from the VS command line).

Edit: forum software doesn't like MSDN URLs with parentheses.
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
I assume you have not created a new type per user, since that would be *insane*

I thus assume that what you *actually* want to do is call a constructor on the class passing in a string to that constructor.

If you know statically the type of the class then you can surely do:

Code:
var user = new UserClass(userName);

If you can't do this it implies that either the constructor isn't public, which is daft, just make it public, or you don't have the type 'UserClass' available to you at compile time. This then means you have an interesting 'chicken/egg' plugin-esque situation and we need way more info to help (William Munny's answer tells you how to call the constructor in that situation but then we have the issue of what you actually do with the returned object).
 

TyphoidMary

Wise, Aged Ars Veteran
154
Ok, I think I have a better handle on the problem now that I have had a few days to not think about it. I know exactly what kind of object I want, I just need to instantiate these objects with unique names that are defined at runtime. Something along the line of given a class and string, return an instance of the class with the specified name. I get a username, and want to create a user object with the username as part of the object name. Something like an object factory, which was what I thought Activator.CreateInstance was. A point of clarification, this an internship for me, they didn't go nearly this far into OOP in my Java classes, which are what we use to teach OOP. This is mostly new materiel for me, This is the first multiuser system I've ever built.
 

Irenicus

Seniorius Lurkius
24
Subscriptor++
You want to create variables like this if I understand you correctly?:

Code:
var userJohn = ...

Why would you do that? I think you just want to create a list/array of users. If you want to be able to quickly access them by username put them in a dictionary with the usernames as keys. It looks like you use C#, then it would be something like:

Code:
Dictionary<String,UserClass> users = new Dictionary<String,UserClass>();

...

// get username
String username = userNameGetter();

// adding a new user (assuming your UserClass has a constructor that accepts the username)
users.Add(username, new User(username));

Then if you want to get a UserClass object for a specific user you can do something like:

Code:
// get user
UserClass user = users[username];

// do stuff with user

Do keep in mind that keys have to be unique in this case.
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
part of the object name

There is no such concept in any OO languages I know.

You can have types which have a field/property/method returning a string that is treated as the 'name' by your program but there is no such concept as a concrete aspect[1] of the language in c#.

if you think Activator.CreateInstance is a general object factory you are going very, very wrong and are seriously out of your depth (which you realise to some degree as you asked the question - great, this step is too much for shed loads of people). But you need to take a step back and express, in rigorous, but plain English what the desired behaviour of the system is which you are trying to achieve and work from there.

Irenicus's answer is a very probable approach to what I _think_ you're trying to do (though there is some subtlety missing there as to what happens if no such user name exists, or you want case insensitive matching) nut you need to start describing what you want to do, not how you're trying to do it, because you're so off base currently answers to that are not going to actually help you.

1. technical term would be reified
 

TyphoidMary

Wise, Aged Ars Veteran
154
Irenicus":3k53jpwc said:
You want to create variables like this if I understand you correctly?:

Code:
var userJohn = ...

Why would you do that? I think you just want to create a list/array of users. If you want to be able to quickly access them by username put them in a dictionary with the usernames as keys. It looks like you use C#, then it would be something like:

Code:
Dictionary<String,UserClass> users = new Dictionary<String,UserClass>();

...

// get username
String username = userNameGetter();

// adding a new user (assuming your UserClass has a constructor that accepts the username)
users.Add(username, new User(username));

Then if you want to get a UserClass object for a specific user you can do something like:

Code:
// get user
UserClass user = users[username];

// do stuff with user

Do keep in mind that keys have to be unique in this case.



This looks like what the solution will be, a thousand thanks be to you! May the largess of a thousand brew-masters ease you burden for the rest of your days.
 

TyphoidMary

Wise, Aged Ars Veteran
154
technophile":1r8krw5u said:
I'm confused. You're doing authentication and roles with AD -- why are you not just using the AD Membership and Role Providers?

Could you elaborate? I'm coming to the .NET world from Java, and this is what my predecessor had decided on as the proper approach, so I'm open to any baked in way to do this.
 

technophile

Ars Legatus Legionis
23,768
TyphoidMary":2q33l46x said:
technophile":2q33l46x said:
I'm confused. You're doing authentication and roles with AD -- why are you not just using the AD Membership and Role Providers?

Could you elaborate? I'm coming to the .NET world from Java, and this is what my predecessor had decided on as the proper approach, so I'm open to any baked in way to do this.
http://msdn.microsoft.com/en-us/library/ff650308.aspx

.NET has an integrated provider model for authentication/role management; it ships with an Active Directory provider that should handle the most common tasks for you. (That said, people seem fairly dissatisfied with the AD role provider; if it doesn't work for you, this looks like a good drop-in replacement.)
 

A.W.

Ars Tribunus Angusticlavius
6,790
TyphoidMary":2eapgzjc said:
technophile":2eapgzjc said:
I'm confused. You're doing authentication and roles with AD -- why are you not just using the AD Membership and Role Providers?

Could you elaborate? I'm coming to the .NET world from Java, and this is what my predecessor had decided on as the proper approach, so I'm open to any baked in way to do this.
If your predecessor also decided on the Activator.CreateInstance approach, I would recommend ignoring all advice that they may have given you, and starting from scratch doing your own research.
 
Status
Not open for further replies.