开发者

How do I get the value of the used parameters in a constructor (C#)

开发者 https://www.devze.com 2023-03-19 22:25 出处:网络
I\'ve got a question about getting the values from a constructor in a generic way. namespace myTestNamespace

I've got a question about getting the values from a constructor in a generic way.

namespace myTestNamespace
{
    Public Class myTestClass()
    {
        Public myTestClass(int myInt,bool myBool, double myDouble)
        {
        //do / set something
        }

        P开发者_如何学Cublic myTestClass(int myInt,bool myBool)
        {
            //do / set something
        }
    }
}

Using (what you need);
Using myTestNamespace;

namespace MyIWannaLookForTheParametersName
{
    Public Class MyLookUpClass()
    {
        Public void DoSomething()
        {
        List<object> myList = new List<object>();

        myTestClass _ myTestClass = new myTestClass(1,true,2.5);
        object mySaveObject = myTestClass;

        mylist.Add(mySaveObject);

        //how do I get the info from the right constructor 
        //(I used the one with 3 parameters_
        //what was the value of myInt, myBool and myDouble
        //how can I make it generic enough, so it will work with other classes with
        // different constructors ass well?
        }
        }
    }


Questions about intent aside, there's no generic way for you to do this. Information about what methods have been called and what values were supplied is not saved automatically. You are, of course, perfectly able to keep track of these things yourself, but you would have to write each class to do this explicitly.

Doing this in a generic way is asking for trouble. What if I did this?

public class Foo
{
    public string Name { get; set; }
}

public class Bar
{
    public Bar(Foo foo)
    {
        // ...
    }
}

Then suppose I called it in this way:

Foo f = new Foo();

f.Name = "Jim";

Bar b = new Bar(f);

f.Name = "Bob";

Now, if such a generic system existed, what would be the value of foo for the Bar constructor? Either it reports "Bob" (which is what the value for Name is on the instance of Foo that was supplied), or it reports "Jim", meaning that the runtime or library would essentially have to be smart enough to make a deep copy of the object so that the state is not changed.

The bottom line is this: if you need access to the parameters passed to the constructor (or any other function), you'll have to store them somewhere explicitly.


You can't get thevalues from the constructor. You need to first place them in a property or a field within your class. The example you provided is a poor use of generics. You wouldbe better off placing the constructor values into properties and creating an interface with those properties.


I got what I needed with this method:

private static ParameterSettings[] GetListOfParametersFromIndicator(object indicatorClass, int loopId, myEnums.ParaOrResult paraOrResult)
    {
        return (from prop in indicatorClass.GetType().GetProperties()
                 let loopID = loopId
                let Indicator = indicatorClass.GetType().Name
                let value = (object)prop.GetValue(indicatorClass, null)
                 where prop.Name.Contains("_Constr_")
                select new ParameterSettings { ParaOrResult=paraOrResult, LoopID= loopId, Indicator= Indicator, ParaName= prop.Name, Value= value }).ToArray();
    }

where ParameterSettings is:

public struct ParameterSettings
    {
        public myEnums.ParaOrResult ParaOrResult    { get; set; }
        public int                  LoopID          { get; set; }
        public string               Indicator       { get; set; }
        public string               ParaName        { get; set; }
        public object               Value           { get; set; }
    }

This info is ok for me. Thanks for the replies.

Regards,

Matthijs

0

精彩评论

暂无评论...
验证码 换一张
取 消