0

Fresh coming in from accepted answer at How do I get object type and passed it on to ObjectContent, which was about passing in class type to newly created function having ObjectContent object in it to work.

My next step is to convert it to static class/functions. To the best of my ability, I'm getting exception error when assigning type.GetMethod("ObjectContent", ...) to methodInfo variable.

The error message is "Object reference not set to an instance of an object".

What seems to be the problem here with type.GetMethod("ObjectContent", ...)? What's the workaround to this problem?

public class XmlError
{
    public string Message { get; set; }
}
public static class XmlBuilderTools 
{
    public static HttpResponseMessage ErrorResponse(object parmXmlErrorLayout)
    {
        HttpResponseMessage httpResponseMsg = new HttpResponseMessage();
        Type type = parmXmlErrorLayout.GetType();  //typeof(parmXmlErrorLayout);

        System.Reflection.MethodInfo methodInfo = type.GetMethod("ObjectContent", (System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.FlattenHierarchy));
        returnHttpResponseMessage.Content = (HttpContent)methodInfo.Invoke(null, new object[] { parmXmlErrorLayout, new XmlMediaTypeFormatter() });

        httpResponseMsg.StatusCode = HttpStatusCode.BadRequest;
    }
}

public class Foo()
{
   public HttpResponseMessage FooFoo()
   {
       //Acutual scripts...
       XmlError xmlError = new XmlError();

       xmlError.Message = "Foo";

       return XmlBuilderTools.ErrorResponse(xmlError);
   }
}

Originally this would work in non-static class/object

returnHttpResponseMessage.Content = new ObjectContent<XmlError>(xmlError, new CustomXmlFormatter());
Community
  • 1
  • 1
fletchsod
  • 3,181
  • 6
  • 31
  • 58
  • Just edited the post to include error messages above. The error message is "Object reference not set to an instance of an object". – fletchsod Dec 10 '14 at 21:36
  • show the ObjectContent method, you sure that is a method and not a property? – T McKeown Dec 10 '14 at 21:36
  • The MSDN showed it is a class name. http://msdn.microsoft.com/en-us/library/system.net.http.objectcontent%28v=vs.118%29.aspx – fletchsod Dec 10 '14 at 21:37
  • ok, that is the class, but you are getting the method from an instance of whatever `parmXmlDataLayout` is... what is the type at run time? You defined it as object... – T McKeown Dec 10 '14 at 21:39
  • I fixed the posting error. It should be parmXmlErrorLayout instead. – fletchsod Dec 10 '14 at 21:44
  • so what is the type? Your code will only work if the class/type has a static method named `ObjectContent()` Show the code where this static method is defined, that is the ONLY way your code will work. – T McKeown Dec 10 '14 at 21:48
  • Hmm. ObjectContent is part of .NET framework and that object is done as instanstantion (instance of object). :-/ – fletchsod Dec 10 '14 at 21:57
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Dec 10 '14 at 22:24
  • It's the internal compoment inside the closed source .NET framework that returned null and it failed to tell us why it is null cuz we can't access closed source to find out why. I found a workaround to it by using a different overloading `ObjectContent` object. By the way, you're not a helpful person for not reading. – fletchsod Dec 12 '14 at 15:52

1 Answers1

1

you have

 Type type = parmXmlDataLayout.GetType();  //typeof(parmXmlDataLayout);

but you pass in

object parmXmlErrorLayout

so shouldnt it be

Type type = parmXmlErrorLayout.GetType();  //typeof(parmXmlErrorLayout);

and if not then you need to define an instance of parmXmlDataLayout as I do not see that anywhere in your code. hence why you would get the error from type.Gettype, type is probably null

Edited by fletchsod - Alternative workaround to the problem

Instead of invoking, appearantly there's another overloading to ObjectContent that works great. Me Duh! Too many of same objects I haven't worked with before. The solution to the problem is

foo.Content = new ObjectContent(parmXmlErrorLayout.GetType(), parmXmlErrorLayout, new CustomXmlFormatter());
fletchsod
  • 3,181
  • 6
  • 31
  • 58
Sorceri
  • 7,482
  • 1
  • 23
  • 37
  • I fixed the posting error. – fletchsod Dec 10 '14 at 21:45
  • ok, on that note, do you check to see if parmXmlErrorLayout is null or type is null after calling parmXmlErrorLayout.GetType()? – Sorceri Dec 10 '14 at 21:47
  • both `type` and `parmXmlErrorLayout` contains data. No nullable value is found. The `ObjectContent` is a class/object found in MSDN documentation at http://msdn.microsoft.com/en-us/library/system.net.http.objectcontent%28v=vs.118%29.aspx . – fletchsod Dec 10 '14 at 21:55
  • as you stated in your comment above its not a static class. Remove System.Reflection.BindingFlags.Static and replace with System.Reflection.BindingFlags.Instance and remove the System.Reflection.BindingFlags.FlattenHierarchy as this is also used for static methods. It looks like you are looking for the constructor which is a public method but is not static. – Sorceri Dec 10 '14 at 22:14
  • Nice. I didn't realize there's "Instance" object. I tried that and still error. Upon further debugging, what I found is `type` contains data but `type.GetMethod(...)` returned null so the variable `methodInfo` end up as null. That explained why I got exception error but why does `GetMethod` returned null? – fletchsod Dec 10 '14 at 22:28
  • 1
    does parmXmlErrorLayout inherit from ObjectContent? if not then you probably want something like typeOf(ObjectContent).GetMethod("ObjectContent", (System.Reflection.BindingFlags.Instance| System.Reflection.BindingFlags.Public)); oh and sorry John hosed you by marking this as a duplicate, I thought it was total BS he did. – Sorceri Dec 10 '14 at 23:09
  • Yes I agree with you about total BS he did. That still doesn't explain why `GetMethod()` is nullable cuz it was done internally inside the .NET framework which I have no control over. Not happy about it but that's called democracy. Let you know very recently I found a different workaround to the problem after many trials & errors. I amended your answer with the workaround I found and I'm gonna accept your answer for all of the trouble we went through. – fletchsod Dec 11 '14 at 14:43
  • While my amended answer is being peer reviewed. The workaround I made is `foo.Content = new ObjectContent(parmXmlErrorLayout.GetType(), parmXmlErrorLayout, new CustomXmlFormatter());` – fletchsod Dec 11 '14 at 14:44