locked
Method with an object as a parameter RRS feed

  • Question

  • can I have a method that would look like this:

    public string aMethod(object parameters)
    {

    }

    Thanks

    Tuesday, May 10, 2011 6:42 PM

Answers

  • Well, do you think that an object is some custom class, or something? like:

      class ClassA
      {
        internal class ClassAA
        {
          public int myKey { get; set; }
          public string myValue { get; set; }
        }
    
        public void SendParameters()
        {
          List<ClassAA> list = new List<ClassAA>();
          list.Add(new ClassAA { myKey = 1, myValue = "value 1" });
          ClassB cB = new ClassB();
          cB.GetParameters(list);
        }
      }
    
      class ClassB
      {
        public void GetParameters(object parameters)
        {
          //use parameters here!
        }
      }
    


    Mitja
    Tuesday, May 10, 2011 7:29 PM
  • It sounds to me like you're looking for Reflection.

    http://msdn.microsoft.com/en-us/library/ms173183(v=VS.100).aspx

    On the bottom of that page, check out the Related Sections.  I think it should be enough to get you banging away.

    Tuesday, May 10, 2011 7:33 PM
  • If you don't know the properties at design time, you can only get at them through reflection. If you know that the object will be one of a few types, you can find the right one using the as keyword. For example:

    public string aMethod(object parameters)
    {
        string val1, val2, val3;
        MyClass1 test1 = parameters as MyClass1;
        if (test1 != null)
        {
            val1 = test1.Property1;
            val2 = test1.Property2;
            val3 = test1.Property3;
        }
        else
        {
            MyClass2 test2 = parameters as MyClass2;
            if (test2 != null)
            {
                val1 = test2.OtherPropertyName1;
                val2 = test2.OtherPropertyName2;
                val3 = test2.OtherPropertyName3;
            }
        }
    }
    

    That is a better route to go if there are only a few options for what the parameter could be. If it really could be a large number of different types of objects, it will get incredibly tedious, but then so could reflection. This may be a situation where you need to revisit your design - are you sure these objects shouldn't all implement a certain interface? Or maybe you should split the code up into separate methods? You can certainly go this route and make it work, but it is likely to come back to bite you.

    Tuesday, May 10, 2011 7:40 PM

All replies

  • Yes, sure why not. But it there is more then one parameter, it has to be an array of objects, like:

    public string aMethod(object[] parameters)
    {
      object A = parameters[0];
      //if you know that thr 1st object in array in an interger then you can convert it:
      int intA = int.Parse(A);
    }
    

     

    So that you can call such a method above, you can do it like:

    //
    int a = 5;
    string b = "aa";
    bool c =false;
    
    //gather all:
    object [] objArray = new object[] { a, b, c };
    aMethod( objArray);
    
    //on the other side you will have parameters array (like in the upper example).
    

     

    Hope it helps,


    Mitja
    Tuesday, May 10, 2011 6:46 PM
  • No, I thought it differently. I am sending just one object and inside of the method I am using that parameter like this:

    paramobject.porpname1

    paramobject.porpname2

    because in the method I would know property names.

    Tuesday, May 10, 2011 6:49 PM
  • Can you show more direct example, please?
    Mitja
    Tuesday, May 10, 2011 6:56 PM
  • well, all I need is to be able to accept an object with properties unknown at design time.

    I dont have more code because I am calling this method using Remoting.

    Tuesday, May 10, 2011 6:59 PM
  • Well, do you think that an object is some custom class, or something? like:

      class ClassA
      {
        internal class ClassAA
        {
          public int myKey { get; set; }
          public string myValue { get; set; }
        }
    
        public void SendParameters()
        {
          List<ClassAA> list = new List<ClassAA>();
          list.Add(new ClassAA { myKey = 1, myValue = "value 1" });
          ClassB cB = new ClassB();
          cB.GetParameters(list);
        }
      }
    
      class ClassB
      {
        public void GetParameters(object parameters)
        {
          //use parameters here!
        }
      }
    


    Mitja
    Tuesday, May 10, 2011 7:29 PM
  • It sounds to me like you're looking for Reflection.

    http://msdn.microsoft.com/en-us/library/ms173183(v=VS.100).aspx

    On the bottom of that page, check out the Related Sections.  I think it should be enough to get you banging away.

    Tuesday, May 10, 2011 7:33 PM
  • If you don't know the properties at design time, you can only get at them through reflection. If you know that the object will be one of a few types, you can find the right one using the as keyword. For example:

    public string aMethod(object parameters)
    {
        string val1, val2, val3;
        MyClass1 test1 = parameters as MyClass1;
        if (test1 != null)
        {
            val1 = test1.Property1;
            val2 = test1.Property2;
            val3 = test1.Property3;
        }
        else
        {
            MyClass2 test2 = parameters as MyClass2;
            if (test2 != null)
            {
                val1 = test2.OtherPropertyName1;
                val2 = test2.OtherPropertyName2;
                val3 = test2.OtherPropertyName3;
            }
        }
    }
    

    That is a better route to go if there are only a few options for what the parameter could be. If it really could be a large number of different types of objects, it will get incredibly tedious, but then so could reflection. This may be a situation where you need to revisit your design - are you sure these objects shouldn't all implement a certain interface? Or maybe you should split the code up into separate methods? You can certainly go this route and make it work, but it is likely to come back to bite you.

    Tuesday, May 10, 2011 7:40 PM