Ahmed Masud
Ahmed Masud

Reputation: 612

Exception occurs when i try to create instance of type through assembly.CreateInstance method

Exception occurs when i try to create instance of type through assembly.CreateInstance method. This is my code in which exception occurs

Assembly assembly = Assembly.LoadFrom(@"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\Microsoft.Office.Server.UserProfiles.dll");
Type type = assembly.GetType("Microsoft.Office.Server.UserProfiles.UserProfileManager");
object ins = assembly.CreateInstance(type.FullName);

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. --->     System.ArgumentNullException: Value cannot be null.
Parameter name: serviceContext
   at Microsoft.Office.Server.UserProfiles.ProfileManagerBase..ctor(SPServiceContext serviceContext)
   at Microsoft.Office.Server.UserProfiles.ProfileManagerBase..ctor(SPServiceContext serviceContext, Boolean ignorePrivacy)
   at Microsoft.Office.Server.UserProfiles.UserProfileManager..ctor(SPServiceContext serviceContext, Boolean IgnoreUserPrivacy, Boolean backwardCompatible)
   at Microsoft.Office.Server.UserProfiles.UserProfileManager..ctor()
   --- End of inner exception stack trace ---
   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
   at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
   at System.Reflection.Assembly.CreateInstance(String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
   at System.Reflection.Assembly.CreateInstance(String typeName)
   at HomeWork.Program.Main(String[] args) in c:\users\amasud\documents\visual studio 2010\Projects\HomeWork\HomeWork\Program.cs:line 23

Upvotes: 1

Views: 10197

Answers (2)

Tony Hopkinson
Tony Hopkinson

Reputation: 20320

From the look of it, whatever type you are creating is expecting a serviceContext parameter in the constructor. You haven't set it, so it crashes.

Upvotes: 1

Cinchoo
Cinchoo

Reputation: 6332

I dont know the reason why you want to create Microsoft.Office.Server.UserProfiles.UserProfileManager object in reflective way. In your case, you have to pass SPServerContext to the constructor. Here is how you can do it.

Assembly assembly = Assembly.LoadFrom(@"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\Microsoft.Office.Server.UserProfiles.dll");
SPServiceContext serviceContext = SPServiceContext.Current;
Type type = assembly.GetType("Microsoft.Office.Server.UserProfiles.UserProfileManager");
object ins = Activator.CreateInstance(type, serviceContext);

Upvotes: 2

Related Questions