简体   繁体   中英

Get FieldInfo of a field that is to be assigned

Is there any way to get FieldInfo of a field in a function that is going to assign a value to that variable?

See my example:

class SomeClass{    
    MyType myObject = SomeOtherClass.MyFunction();
}

class SomeOtherClass{
    public static MyType MyFunction(){
        //Get FieldInfo about myObject here
    }
}

Background:

What I want to do is to be able to set an attribute on "myObject" which specifies that the object should be cached in "MyFunction".

Sorry Herber, I tried responding in a comment but this was to large to work as a comment:

In the case you mentioned in response to my last reply, does this work for you?

    class Program
    {
        static void Main(string[] args)
        {
            SomeClass sc = new SomeClass();
        }
    }
    class SomeClass
    {
        public MyType myObject;
        public SomeClass()
        {
            SomeOtherClass.MyFunction(this);
        }
    }

    static class SomeOtherClass
    {
        public static void MyFunction(SomeClass sClass)
        {
            sClass.myObject = new MyType() { Name = "Test1" };
            FieldInfo[] fInfo = sClass.myObject.GetType().GetFields();
            Console.WriteLine(fInfo[0].GetValue(sClass.myObject));
        }
    }
    class MyType
    {
        public string Name;
    }

This is completely impossible.

When the function is called, it has no awareness of what you're going to do with the result.

There isn't any way you can do it using the assignment operator. The MyFunction function requires a reference to the myObject object to be able to determine the field info.

Your best bet is to either check before the assignment or to pass myObject into MyFunction

Not sure if this is what your're after: But you could try using the an "out" parameter so as the method you are calling has knowledge of target of the assignment? like so:

class SomeClass
{
    MyType myObject;
    public SomeClass()
    {
        SomeOtherClass.MyFunction(out myObject);
    }
}

static class SomeOtherClass
{
    public static void MyFunction(out MyType mType)
    {
        mType = new MyType();
        FieldInfo[] fInfo = mType.GetType().GetFields();
    }
}
class MyType
{
    string Name;
}

Hope that helps :¬)

OK one last stab before I call it a night...

class Program
{
    static void Main(string[] args)
    {
        SomeClass sc = new SomeClass();            
    }
}

[AttributeUsage(AttributeTargets.Field)]
public class MyAttribute : System.Attribute
{
    public readonly bool Foo;
    public MyAttribute(bool foo)
    {
        Foo = foo;
    }

}
class SomeClass
{
    [MyAttribute(true)]
    public MyType myObject;
    [MyAttribute(true)]
    public int myInt;
    public bool myBool;
    public SomeClass()
    {
        SomeOtherClass.MyFunction(this);
    }
}

static class SomeOtherClass
{
    public static void MyFunction(SomeClass sClass)
    {
        sClass.myObject = new MyType() { Name = "Test1"};
        foreach(FieldInfo finfo in  GetFeilds(sClass))
            Console.WriteLine(finfo.GetValue(sClass));
    }

    public static IEnumerable<FieldInfo> GetFeilds(SomeClass sClass)
    {
        foreach (FieldInfo field in typeof(SomeClass).GetFields())
        {
            foreach (Attribute attr in field.GetCustomAttributes(true))
            {
                if (field.GetCustomAttributes(typeof(MyAttribute), true)!= null && ((MyAttribute)attr).Foo)
                    yield return field;
            }
        }            
    }
}

class MyType
{
    public string Name;
}   

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