Zed McJack
Zed McJack

Reputation: 61

How to return value from form created with Activator.CreateInstance

I need to get the return value from the form created with Activator.CreateInstance, but although the return value is set as public variable I only get the error in the calling form. Here is the code

    private void zButtonInsert1_Click(object sender, EventArgs e)
    {
        var type = Type.GetType("MyNamespace." + this.form_name);
        using (var form = Activator.CreateInstance(type, UIB, ID) as Form)
        {
            if (form != null)
                form.ShowDialog();
                 this.ID = form._id; //Here I got the error
        }
    }

Here is the error message

'Form' does not contain a definition for '_id' and no extension method '_id' accepting a first argument of type 'Form' could be found (are you missing a using directive or an assembly reference?)

In the called form the value (lastID) is assigned to public variable

  _id = (int)kon.lastID;

Upvotes: 2

Views: 1049

Answers (3)

Reza Aghaei
Reza Aghaei

Reputation: 125217

It's better to work with typed objects. But here I suppose you want to create object using type name and you don't want / can't to cast the object to desired type.

Even in this case, since you know your forms have an _id field, you can force them to create Id property and in fact force them to implement an interface and when reading value, cast it to he interface.

For example

public interface IFormWithId
{
    int Id {get;set;}
} 

And implement interface for your classes:

public partial class SomeForm: IFormWithId
{
    public int Id {get;set;}    
    public SomeForm(int id)
    {
        InitializeComponent();
        Id = id;
    }
}

Then when reading value use such code:

var id = ((IFormWithId)form).Id;

If for any reason you don't want to use interfaces, you can consider these options:

If _id is private or protected field

If _id is private or protected field and the form doesn't have a property which exposes the field, then the only solution to find its value is using reflecten. For example:

var field = form.GetType().GetField("_id", /*Use GetProperty for property*/
        System.Reflection.BindingFlags.NonPublic |
        System.Reflection.BindingFlags.Instance);
var value = field.GetValue(form);

If _Id is a public field or property

You can use dynamic this way:

var id = ((dynamic)form)._id;

Upvotes: 1

Deepak Sharma
Deepak Sharma

Reputation: 4170

Actually you are typecasting your instance as Form and that by default doesn't have any property with name _id so you are getting error.

You can use reflection to get the property.

private object GetProperty(Form obj, string propName)
{
   var p = ob.GetType().GetProperty(propName);
   if(p != null)
   {
       return p.GetValue(ob);
   }
   return null;
}

and then you can call in your code like below -

if (form != null)
{
   form.ShowDialog();
   object val = GetProperty(form, "_id");
   this.ID = TypeCastValHere(val);
}

don't forget to type cast the return value from object to `this.ID' type

Upvotes: 1

Vecchiasignora
Vecchiasignora

Reputation: 1315

You should use get property, field .... like this

var value = type.GetProperty("SomeProperty").GetValue(object)...;

Upvotes: 1

Related Questions