3 Replies - 683 Views - Last Post: 09 May 2016 - 02:35 PM Rate Topic: -----

#1 Damage   User is offline

  • Lord of Schwing
  • member icon

Reputation: 300
  • View blog
  • Posts: 1,989
  • Joined: 05-June 08

Check if Object type is a stream

Posted 05 May 2016 - 06:31 PM

Is there a way to check if an Object type is some sort of stream?

We have a generic method that passes the data along and "jsonify's" that data, but that fails if the object is a stream

Using reflection i can check the type and am currently(using magic strings which i don't like) checking against multiple types.
Ideally i'd just like to be able to check against a base stream type, if such a thing exists?

so this is what i have
transModel.Xml = trans.Data.GetType().ToString() == "TCCIntegration.Business.EDRMSDocumentStream" ? Jsonhelper.JsonifyObject("Dummy XML") : Jsonhelper.JsonifyObject(trans.Data);



this is what i'd like

transModel.Xml = trans.Data.GetType() == System.IO.Stream ? Jsonhelper.JsonifyObject("Dummy XML") : Jsonhelper.JsonifyObject(trans.Data);



Is This A Good Question/Topic? 0
  • +

Replies To: Check if Object type is a stream

#2 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7136
  • View blog
  • Posts: 24,244
  • Joined: 05-May 12

Re: Check if Object type is a stream

Posted 05 May 2016 - 09:23 PM

There is the is keyword to see if that object is a particular object type, subclass of the type, or if it implements an interface.
Was This Post Helpful? 1
  • +
  • -

#3 Damage   User is offline

  • Lord of Schwing
  • member icon

Reputation: 300
  • View blog
  • Posts: 1,989
  • Joined: 05-June 08

Re: Check if Object type is a stream

Posted 08 May 2016 - 01:56 PM

i'll give it a look thanks
Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Check if Object type is a stream

Posted 09 May 2016 - 02:35 PM

is is the correct way to do this.

Example:

object x = new FileStream(@"c:\scratch\garbage.csv", FileMode.Open);
object y = new MemoryStream();
object z = "this is a string";
Console.WriteLine("is x a stream? {0}", x is Stream);
Console.WriteLine("is y a stream? {0}", y is Stream);
Console.WriteLine("is z a stream? {0}", z is Stream);
(x as IDisposable).Dispose();
(y as IDisposable).Dispose();


Output:
is x a stream? True
is y a stream? True
is z a stream? False

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1