Assembly.CreateInstance Method (String) not work for System.Windows object
-
quarta-feira, 18 de abril de 2012 11:26
I have one dll (System.Windows). And i am trying to create TextBlock object. but CreateInstance throw error. below i added sample code and exception.
Code Sample....
try
{
System.Reflection.Assembly extAssembly;
Object objExtAssembly;
Type tpExtAssembly;
extAssembly = System.Reflection.Assembly.LoadFrom("dllPath");
tpExtAssembly = extAssembly.GetType("System.Windows.Controls.TextBlock");// opt1>>
objExtAssembly = (object)Activator.CreateInstance(tpExtAssembly);
// opt2>>
objExtAssembly = extAssembly.CreateInstance(tpExtAssembly.Namespace + "." + tpExtAssembly.Name);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message );
}If i am using other .net dll than its works fine. If i am doing wrong than let me know
Thanx.....
----------- Error in Code "objExtAssembly = extAssembly.CreateInstance(tpExtAssembly.Namespace + "." + tpExtAssembly.Name);"
System.Reflection.TargetInvocationException was caught
HResult=-2146232828
Message=Exception has been thrown by the target of an invocation.
Source=mscorlib
StackTrace:
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 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 SilverlightClassLibrary1.Class1.CallTimer() in D:\My Documents\Visual Studio 2010\Projects\loadObjectTest\SilverlightClassLibrary1\Class1.cs:line 32
InnerException: System.TypeInitializationException
HResult=-2146233036
Message=The type initializer for 'MS.Internal.JoltHelper' threw an exception.
Source=System.Windows
TypeName=MS.Internal.JoltHelper
StackTrace:
at MS.Internal.JoltHelper.get_ThreadID()
at MS.Internal.XcpImports.CheckThread()
at MS.Internal.ManagedPeerHelper.SetupManagedPeer(IManagedPeerBase managedPeer, NativeObjectSafeHandle& nativeSafeHandle, UInt32 nativeTypeIndex, IntPtr constructDO, Nullable`1 isCustomType, Nullable`1 forceStrong)
at System.Windows.DependencyObject..ctor(UInt32 nativeTypeIndex, IntPtr constructDO)
at System.Windows.DependencyObject..ctor(UInt32 nativeTypeIndex)
at System.Windows.UIElement..ctor(UInt32 nKnownTypeIndex)
at System.Windows.FrameworkElement..ctor(UInt32 nKnownTypeIndex)
at System.Windows.Controls.TextBlock..ctor()
InnerException: System.IO.FileNotFoundException
HResult=-2147024894
Message=Could not load file or assembly 'System.Net, Version=5.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' or one of its dependencies. The system cannot find the file specified.
Source=System.Windows
FileName=System.Net, Version=5.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e
FusionLog==== Pre-bind state information ===
LOG: User = AUTOMATION\gaurang.patel
LOG: DisplayName = System.Net, Version=5.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e
(Fully-specified)
LOG: Appbase = file:///D:/My Documents/Visual Studio 2010/Projects/loadObjectTest/loadObjectTest/bin/Debug/
LOG: Initial PrivatePath = NULL
Calling assembly : System.Windows, Version=5.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e.
===
LOG: This bind starts in default load context.
LOG: No application configuration file found.
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: System.Net, Version=5.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e
LOG: Attempting download of new URL file:///D:/My Documents/Visual Studio 2010/Projects/loadObjectTest/loadObjectTest/bin/Debug/System.Net.DLL.
LOG: Attempting download of new URL file:///D:/My Documents/Visual Studio 2010/Projects/loadObjectTest/loadObjectTest/bin/Debug/System.Net/System.Net.DLL.
LOG: Attempting download of new URL file:///D:/My Documents/Visual Studio 2010/Projects/loadObjectTest/loadObjectTest/bin/Debug/System.Net.EXE.
LOG: Attempting download of new URL file:///D:/My Documents/Visual Studio 2010/Projects/loadObjectTest/loadObjectTest/bin/Debug/System.Net/System.Net.EXE.
StackTrace:
at MS.Internal.JoltHelper..cctor()
InnerException:
------------
Todas as Respostas
-
quarta-feira, 18 de abril de 2012 12:02
Hi,
first you should think about the namespaces you are using. It is recommend to use your own namespace-names and not use namespaces of others for your own classes (so no System.*).
In general the core problem seems to be:
Could not load file or assembly 'System.Net, Version=5.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' or one of its dependencies. The system cannot find the file specified.So maybe your System.Windows DLL has a reference to System.Net and you simply didn't provide that DLL?
With kind regards,
Konrad
- Sugerido como Resposta Anirban Bhattacherya quarta-feira, 18 de abril de 2012 13:09
- Marcado como Resposta Mike FengMicrosoft Contingent Staff, Moderator sexta-feira, 27 de abril de 2012 11:47

