0

I have some classes:

    public class clsAnimal
        {
            public int ID { get; set; }
            public string Strain { get; set; }
        }

     public class clsHybridom
        {
            public int ID { get; set; }
            public string Haplo { get; set; }
        }

Those classes are instanced into session:

Session["clsAnimal"] = new clsAnimal();

and accessed like (if exist better method, please let me know):

((clsAnimal)Session["clsAnimal"])

There is a lot of classes like these two, I have a switch and by request instancing required class. Instanced class should be parsed into MyFunction .

There is public static class with functions used in whole project and in this class should be my function MyFunction

But alas! I don't know how to parse those classes into function MyFunction

My try was something like:

ASMX:

object ReturnObject;
Session["clsAnimal"] = new clsAnimal();
ReturnObject = Activator.CreateInstance(((clsAnimal)Session["clsAnimal"]));

return clsDataProc.MyFunction(ReturnObject); 

public static class:

 public static string MyFunction( object o )
    {
       return Do_Some_Evil_Things_With_Parsed_Class();
    }

Obviously that is not going to happen because:

  • cannot convert from 'clsAnimal' to 'System.Type'
  • The best overloaded method match for 'System.Activator.CreateInstance(System.Type, params object[])' has some invalid arguments

Long story short: how to parse instanced class into public static string MyFunction?

dllhell
  • 1,987
  • 3
  • 34
  • 51
  • Why don't you pass the typeof(clsAnimal) as a second parameter to your xmlserializer? – Robert Aguilar Jul 21 '14 at 18:38
  • 1
    And why not stop prefixing all classes with `cls`? Being a class isn't such a special thing that you need to prefix all classes, just to remind you that they are classes. – John Saunders Jul 21 '14 at 18:46
  • 1
    Agree with JohnSaunders, with modern IDE's in type-safe programming languages Hungarian Notation is ridiculous, even more so on **types**... – Bas Jul 21 '14 at 18:55
  • possible duplicate of [Serialize an object to string](http://stackoverflow.com/questions/2434534/serialize-an-object-to-string) – Philip Pittle Jul 21 '14 at 19:14

2 Answers2

1

It sounds like you are trying to Xml serialize an object to a string. So then you are trying to construct a System.Xml.Serialization.XmlSerializer. It wants to know the type of the object to be serialized. If you know the type of object at compile time, which it seems like you can do, you do this:

clsAnimal a = ((clsAnimal)Session["clsAnimal"]);
var serializer = new XmlSerializer(typeof(clsAnimal));
serializer.Serialize(..., a);

where ... is your StringStream or whatever. I figure you have that part down. If you do not know the type at compile time, you use GetType()

object o = Session["clsAnimal"]; // No idea what this type is!
var serializer = new XmlSerializer(o.GetType()); // That's okay, just get the type at runtime
serializer.Serialize(..., o);

If you want to wrap that in a function, there are two ways to get the type:

1) Use a generic. Then you can get the type as typeof(T):

public static string XMLSerialize<T>(T objectToSerialize)

2) Use object, then GetType()

public static string XMLSerialize(object objectToSerialize)

I hope I understood the question well enough to help.

Moby Disk
  • 3,761
  • 1
  • 19
  • 38
  • Obviously I made a mistake putting information about my intention with those classes in my question. Please ignore serializer. Question is: "How to parse a class into function as an argument?" – dllhell Jul 21 '14 at 19:26
  • you can pass "an instance of a class" into a function, or you can pass the "type" of the class into a function. GetType() does that. – Moby Disk Jul 21 '14 at 19:29
0

Maybe another solution here would be to use an interface that all your classes inherit from.

Public interface IMyfunctionInterface
{
  public MyFunction();
}

And then you could do something like this with all your classes that inherit from this interface:

Session["clsAnimal"] = new clsAnimal();
((IMyfunctionInterface)Session["clsAnimal"]).MyFunction();

Just a thought...

codea
  • 1,439
  • 1
  • 17
  • 31