Tricky question. The problem is that you can actually load an assembly multiple times. Most of the time assemblies are loaded using Assembly.Load() which will load an assembly only once. However if you call Assembly.LoadFile() then you can load the same assembly from different paths at the same time. Needless to say this can cause problems. Assembly.LoadFrom() can also be used but it can cause similar problems. Just a warning.
To enumerate the assemblies loaded in an AppDomain use the following code.
private void LoadAssemblies ( ) { Assembly[] asms = AppDomain.CurrentDomain.GetAssemblies(); foreach (Assembly asm in asms) { //Do something with the assembly }; }
I don't know what you are doing with FormatterServices but if you want to determine whether a type is implemented in the assembly then use Assembly.GetType(string) on the assembly object. If you already have an instance of the type and you want to know who implemented it then you can use Type.Assembly like so:
Type type = someAsm.GetType("MyType"); type.Assembly == someAsm;
foreach(Assembly assembly in assemblies) { if (assembly.FullName == fullName) { lAssembly = assembly; return true; } }
lAssembly = null; return false; }
And it returns a Assembly object as well,but not sure whether it returns the expected Assembly.Because when i check whether a Type(which is part of this assembly) using FormatterServices.GetTypeFromAssembly,it returns a null.
Well,I'm trying to learn serialization by trying out a IniFormatter.I manage to finish the Serialize method and trying to make DeSerialize method.Its a simple formatter where i doesnt prepare object graph etc., here is the DeSerialize code..
public object Deserialize(string filename) { if (filename == null) throw new Exception("File name cannot be null!");
I would verify that your assembly is correctly named and that the type name is the fully qualified path to your class. It is possible that either the assembly you are referencing isn't the one containing the type or the type name is not fully qualified. Either one will cause a problem. If they are accurate then look at the list of loaded assemblies through the debugger to verify that the assembly you expected was loaded. If that works then try a direct call to Activator.CreateInstance to see if it'll work.
Check if Assembly is already loaded into AppDomain
JesseStC
To enumerate the assemblies loaded in an AppDomain use the following code.
private void LoadAssemblies ( )
{
Assembly[] asms = AppDomain.CurrentDomain.GetAssemblies();
foreach (Assembly asm in asms)
{
//Do something with the assembly
};
}
I don't know what you are doing with FormatterServices but if you want to determine whether a type is implemented in the assembly then use Assembly.GetType(string) on the assembly object. If you already have an instance of the type and you want to know who implemented it then you can use Type.Assembly like so:
Type type = someAsm.GetType("MyType");
type.Assembly == someAsm;
Michael Taylor - 10/21/05
simonech
private bool IsAssemblyLoaded(string fullName,out Assembly lAssembly)
{
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach(Assembly assembly in assemblies)
{
if (assembly.FullName == fullName)
{
lAssembly = assembly;
return true;
}
}
lAssembly = null;
return false;
}
And it returns a Assembly object as well,but not sure whether it returns the expected Assembly.Because when i check whether a Type(which is part of this assembly) using FormatterServices.GetTypeFromAssembly,it returns a null.
Thanks,
Suresh.
Pat Long
public object Deserialize(string filename)
{
if (filename == null)
throw new Exception("File name cannot be null!");
IniReader iReader = new IniReader(filename);
string assemblyName = iReader.ReadString("Assembly","FullName");
Assembly myAssembly;
if (!IsAssemblyLoaded(assemblyName,out myAssembly))
myAssembly = AppDomain.CurrentDomain.Load(assemblyName);
string typeName = iReader.ReadString("Type","Name");
Type myType = FormatterServices.GetTypeFromAssembly(myAssembly,typeName);
object myObject = FormatterServices.GetUninitializedObject(myType);
MemberInfo[] mInfo = FormatterServices.GetSerializableMembers(myType);
object[] objInfo = new object[mInfo.GetLength(0)];
for(int i=0;i<mInfo.GetLength(0);i++)
{
objInfo
}
return FormatterServices.PopulateObjectMembers(myObject,mInfo,objInfo);
}
Here Type myType = FormatterServices.GetTypeFromAssembly(myAssembly,typeName); is getting null reference.But IsAssemblyLoaded function return true.
Suresh.
Richard Hardy
Anirshk
Michael Taylor - 10/24/05
BlackKnight_Ve
But the answer
Suresh.