none
"Non-static method requires a target." error when entity has a property that returns an interface.

    Question

  • I have set the entities in my client entities to inherit an interface.
    I have used a partial class to define a new property for one of my entities:

            public ISolution IParentSolution  
            {  
                get 
                {  
                    return (ISolution)this.ParentSolution;  
                }  
                set 
                {  
                    this.ParentSolution = (Solution)value;  
                }  
            }  
     

    I then run the following code:

                foreach (SoPoSystem s in this.entities.SoPoSystem)  
                {  
                    this.entities.LoadProperty(s, "Solutions");  
                    foreach (Solution sol in s.Solutions)  
                    {  
                        sol.SolutionName = "My New Name" + DateTime.Now.ToString();  
                        this.entities.UpdateObject(sol);  
                        this.entities.SaveChanges();  
                    }  
                }  
     
      When I run this I get the following error:


    System.Reflection.TargetException occurred
      Message="Non-static method requires a target."
      Source="mscorlib"
      StackTrace:
           at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)
           at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
           at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
           at System.Data.Services.Client.DataServiceContext.WriteContentProperties(XmlWriter writer, ClientType type, Object resource)
           at System.Data.Services.Client.DataServiceContext.WriteContentProperties(XmlWriter writer, ClientType type, Object resource)
           at System.Data.Services.Client.DataServiceContext.CreateRequestData(ResourceBox box, Boolean newline)
           at System.Data.Services.Client.DataServiceContext.SaveAsyncResult.CreateChangeData(Int32 index, Boolean newline)
           at System.Data.Services.Client.DataServiceContext.SaveAsyncResult.BeginNextChange(Boolean replaceOnUpdate)
           at System.Data.Services.Client.DataServiceContext.SaveChanges(SaveChangesOptions options)
           at System.Data.Services.Client.DataServiceContext.SaveChanges()
           at Protiviti.SoPo.Client.Modules.MethodologyManager.Views.MethodologyManagerTree.MethodologyManagerTreePresentationModel..ctor(IMethodologyManagerTreeView view) in D:\Projects\Protiviti\Protiviti.SoPo\Protiviti.SoPo.Client.Modules.MethodologyManager\Views\MethodologyManagerTree\MethodologyManagerPresentationModel.cs:line 47
      InnerException:

    If I comment out the property I don't get this error.

    I also have the property below and it does not cause this error:

            public System.Collections.ObjectModel.Collection<IMethodology> IMethodologies  
            {  
                get 
                {  
                    return new System.Collections.ObjectModel.Collection<IMethodology>(this.Methodologies.Cast<IMethodology>().ToList<IMethodology>());  
                }  
                set 
                {  
                    this.Methodologies = new System.Collections.ObjectModel.Collection<Methodology>(value.Cast<Methodology>().ToList<Methodology>());  
                }  
            }  
     

    Any idea what is causing this error?

    Thanks!
    Stewart
    Tuesday, January 06, 2009 5:29 PM

All replies

  • I have also figured out that if I force it to return a value instead of a null value I do not get the error.

            [DoNotSerialize]  
            public ISolution IParentSolution  
            {  
                get 
                {  
                    return (ISolution)new Solution();  
                }  
                set 
                {  
                    this.ParentSolution = (Solution)value;  
                }  
            }  
     

    Notice:
    return (ISolution)new Solution();
    Instead of:

    return (ISolution)this.ParentSolution;

    The latter returns null.
    Tuesday, January 06, 2009 5:56 PM
  • After playing around with this for a while I think I have identified the problem.  It looks like something in the SaveChanges is using reflection to get the value of a property that returns an interface.  When the property returns a null value for the interface the exception is thrown.  I don't know enough about reflection or the SaveChanges architecture to know what is causing this.

    Is this a bug?  Is the ado.net data services framework doing something with properties that return null values of a class type that it is not doing with properties that return a null value of a interface type?

    To work around this I have changed the few properties that return interfaces that could have a null value to be of a regular entity type.

    I was trying to wrap the data context and all entities generated by ado.net data services with interfaces so that all interaction with it by my presentation model and view would be via the interface.  I was hoping to be able to swap in another data model that implements the same interface.  Maybe I am approaching this all wrong.  If you can think of another way I can achieve this please let me know.

    Thanks,
    Stewart
    Tuesday, January 06, 2009 9:09 PM
  • Does anyone have any idea on this?  I figured there might be another way for me to approach this but I still haven't found an answer.
    Sunday, February 01, 2009 9:44 AM
  • Hi Stewart,

    I don't have a solution to this problem, but your post helped me in that it told me what the cause of the problem is.

    I'm sure you saved me some time: thanks!

    Ben
    Sunday, February 01, 2009 8:40 PM