views:

44

answers:

5

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".

+2  A: 

This is completely impossible.

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

SLaks
I suspected as much, thanks for the quick answer.
Herber
@Herber: if you like an answer you can upvote and/or even accept it ^^
tanascius
I'm still on the lookout for a possible workaround, so the accept will have to wait :)
Herber
@Herber: As a workaround, you can use pass a parameter to the function instead of using an attribute.
SLaks
That would work if the argument was passed ByRef. But then I could just as well pass a bool as an argument and skip using reflection at all..
Herber
@Herber: I meant a boolean or enum parameter. It is not possible to get a FieldInfo from a `ref` parameter. Also, making a function that reads an attribute from its callsite is a terrible design.
SLaks
Sorry, I misread. That seems to be the solution in this case, yes.
Herber
A: 

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

lomaxx
A: 

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 :¬)

jdoig
I'm looking for the FieldInfo of the "myObject" field in "SomeClass", not all FieldInfos in "MyType", sorry if I was unclear.
Herber
+2  A: 

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;
    }
jdoig
That's more like what I'm looking for, I'll give you an upvote. However, as I mentioned in a comment to SLaks, then I could as well pass a bool to "MyFunction" and skip using reflection alltogether..
Herber
A: 

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;
}   
jdoig