简体   繁体   中英

How to get return values from list of functions containing multiple types in C#?

I'm trying to create a list that stores references to variables in other parts of my project, so that I can then use this list to access their up-todate values at any time. To complicate things, these can be of any type.

After reading on the second response here , I ended up creating two classes, the first of which stores functions that return variable values:

public class Datum<T>{
    public string name {get; set;}
    public Func<T> getVal;

    public Datum(string name, Func<T> getVal){
        this.name = name;
        this.getVal = getVal;
    }
    public T Value { get { return getVal (); }}
}

And the second of which stores these data pieces in a list:

    public class DataObject{
    public List<object> dataList { get; set; }
    public DataObject(){
        this.dataList = new List<object>();
        this.typeList = new List<string>();
    }
}

Then I can instantiate the DataObject:

    testList = new DataObject();

And use a lambda function to pass a reference to a variable I care about:

    testList.dataList.Add (new Datum<float> ("MyVariableName", () => myVariable));

So, I can now add as many pieces of data of different types as I want. The trouble that I am having is that I can't figure out how to get at the data without creating a new variable:

    Datum<float> thisDatum= (Datum<float>)testList.dataList [0];
    print (thisDatum.name + thisDatum.Value);

Is there a way I can get Value to return without creating a new variable? I'll be doing this for some data every single frame, so it seems memory-intensive to keep creating new variables this way. Any ideas? Thanks!

EDIT: Just to be clear, the reason I am passing in functions is so that I can reference the current value of myVariable at any time I want. For example, if MyVariable is a reference to the number of frames since the start of runtime, then calling thisDatum.Value should return a different value on each frame. Passing the functions in this way works, as shown in the linked answer (I have verified this), but that method does not allow for easy access to variables of different types.

I guess, this will work:

public class Datum<T>{
    public string name {get; set;}
    public T value;

    public Datum(string name, Func<T> getVal){
        this.name = name;
        this.value = getVal();
    }
    public T Value { get { return value; }}
}

A propos, why do you need to pass a function to the costructor. Why wouldn't you pass the value instead?

  public Datum(string name, T value){ this.name = name; this.value = value; } ..... var myDatum = new Datum("Name", myVariable); 

Based on your implementation, where your function only return value

testList.dataList.Add (new Datum<float> ("MyVariableName", () => myVariable));

you don't need whole class at all and instead of generic you can use object type for values. You can use Dictionary<string, object>

var testList = new Dictionary<string, object>
{
    { "MyVariableName", myStringVariable },
    { "MyAnotherVariableName", myFloatVariable }
};

Then use it, of course you need cast it to the type you expecting.

float valueToUse = (float)testList.["MyAnotherVariableName"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM