views:

2222

answers:

3

I've the following class in XmlSer.dll

namespace xmlser
{
        public class XmlSer
        {
                public Type test(string s)
                {
                    return Type.GetType(s);
                }

        //...other code

        }
}

and the following code in MyApp.exe, which links XmlSer.dll as a reference

namespace MyApp
{
    public class TestClass
    {
        public int f1 = 1;
        public float f2 = 2.34f;
        public double f3 = 3.14;
        public string f4 = "ciao";
    }

    class MainClass
    {

        public static void Main(string[] args)
        {
            TestClass tc = new TestClass();
            XmlSer ser = new XmlSer();
            Console.WriteLine(ser.test("MyApp.TestClass")!=null);
        }
}

Running MyApp.exe I get false, that means that ser instance of XmlSer is not able to get the type of Testclass (result is null). Putting XmlSer class directly in MyApp.exe code I correctly get the type of TestClass.

Checking on the net i've found that the problem is related to the assemblies. That means, the assembly of .exe is not visible to the XmlSer.test method so it can't resolve the type of TestClass.

How can I resolve the problem maintaing XmlSer in XmlSer.dll and MyApp.MainClass in MyApp.exe ?

Thanks.

Alessandro

+3  A: 

Not sure, but maybe try ser.Test("MyApp.TestClass, MyApp")?

EDIT: Obviously MyApp not XmlSer

+6  A: 

Since the two are not in the same assembly, you probably need to include the assembly name in the type string:

Console.WriteLine(ser.test("MyApp.TestClass, MyApp")!=null);


If all you want to do is serialize arbitrary objects, you can do the following:

public static class Serialization
{
    public static void Serialize(object o, Stream output)
    {
        var serializer = new XmlSerializer(o.GetType());
        serializer.Serialize(output, o);
    }
}
John Saunders
How can I get the ID of the caller assembly (MyApp in this case) ?What about If I'd like to recognize the type contained in a second .dll ?Is there no way to autodetect the assembly of a given type ?I'm looking for something like a global GetType which recognize the type in all assemblies.Thanks
alexroat
You don't get the id of the caller. The caller knows the assembly of the caller, hence the assembly of the type. You don't want a method that searches every assembly. There could be very many of them, and you might have to load metadata to do the search.
John Saunders
What is your overall goal here? What are you trying to accomplish? Your class is named "XmlSer". Are you trying to do XML Serialization? Please give some context, and we may be better able to help.
John Saunders
Hi,sorry for the late.Yes I've written an xml serializer because of the serialize of .NEt is type dependent. I'm building a serializer which doesn't know which kind of object it will deserialize so that's the point.However, I implemented my class with both know and unknown assembly.
alexroat
Every object has a GetType() method. All you have to do is "new XmlSerializer(o.GetType())". You don't need to create the type from a string.
John Saunders
A: 

Hi, I've found a definitive solution. I simply get the vector of all assemblies loaded from application. Then on each assembly I call Type.GetType till I get a valid Type. In this way you can obtain every type which is loaded in your process space regardless the assembly.

This below is the code of the function.

public static Type GetGlobalType(string s)
        {
            Type t=null;
            Assembly[] av = AppDomain.CurrentDomain.GetAssemblies();
            foreach (Assembly a in av)
            {
                t=Type.GetType(s + "," + a.GetName());
                if (t != null)
                    break;
            }
            return t;
        }

Cheers :)

Alessandro

alexroat
What happens if there are a large number of assemblies loaded into the AppDomain?
John Saunders
Simply, it will require long time to resolve the type with reflection.In case of multiple declaration it will take the first occurrence of type in AppDomain and this behaviour is exactly the same of a java application which links multiple jars, that is what I was looking for.
alexroat