Extension methods must be defined in a non-generic static class












176















I'm getting the error:




Extension methods must be defined in a non-generic static class




On the line:



public class LinqHelper


Here is the helper class, based on Mark Gavells code. I'm really confused as to what this error means as I am sure it was working fine when I left it on Friday!



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Linq.Expressions;
using System.Reflection;

/// <summary>
/// Helper methods for link
/// </summary>
public class LinqHelper
{
public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "OrderBy");
}
public static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "OrderByDescending");
}
public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "ThenBy");
}
public static IOrderedQueryable<T> ThenByDescending<T>(this IOrderedQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "ThenByDescending");
}
static IOrderedQueryable<T> ApplyOrder<T>(IQueryable<T> source, string property, string methodName)
{
string props = property.Split('.');
Type type = typeof(T);
ParameterExpression arg = Expression.Parameter(type, "x");
Expression expr = arg;
foreach (string prop in props)
{
// use reflection (not ComponentModel) to mirror LINQ
PropertyInfo pi = type.GetProperty(prop);
expr = Expression.Property(expr, pi);
type = pi.PropertyType;
}
Type delegateType = typeof(Func<,>).MakeGenericType(typeof(T), type);
LambdaExpression lambda = Expression.Lambda(delegateType, expr, arg);

object result = typeof(Queryable).GetMethods().Single(
method => method.Name == methodName
&& method.IsGenericMethodDefinition
&& method.GetGenericArguments().Length == 2
&& method.GetParameters().Length == 2)
.MakeGenericMethod(typeof(T), type)
.Invoke(null, new object { source, lambda });
return (IOrderedQueryable<T>)result;
}
}









share|improve this question





























    176















    I'm getting the error:




    Extension methods must be defined in a non-generic static class




    On the line:



    public class LinqHelper


    Here is the helper class, based on Mark Gavells code. I'm really confused as to what this error means as I am sure it was working fine when I left it on Friday!



    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Linq.Expressions;
    using System.Reflection;

    /// <summary>
    /// Helper methods for link
    /// </summary>
    public class LinqHelper
    {
    public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string property)
    {
    return ApplyOrder<T>(source, property, "OrderBy");
    }
    public static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> source, string property)
    {
    return ApplyOrder<T>(source, property, "OrderByDescending");
    }
    public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source, string property)
    {
    return ApplyOrder<T>(source, property, "ThenBy");
    }
    public static IOrderedQueryable<T> ThenByDescending<T>(this IOrderedQueryable<T> source, string property)
    {
    return ApplyOrder<T>(source, property, "ThenByDescending");
    }
    static IOrderedQueryable<T> ApplyOrder<T>(IQueryable<T> source, string property, string methodName)
    {
    string props = property.Split('.');
    Type type = typeof(T);
    ParameterExpression arg = Expression.Parameter(type, "x");
    Expression expr = arg;
    foreach (string prop in props)
    {
    // use reflection (not ComponentModel) to mirror LINQ
    PropertyInfo pi = type.GetProperty(prop);
    expr = Expression.Property(expr, pi);
    type = pi.PropertyType;
    }
    Type delegateType = typeof(Func<,>).MakeGenericType(typeof(T), type);
    LambdaExpression lambda = Expression.Lambda(delegateType, expr, arg);

    object result = typeof(Queryable).GetMethods().Single(
    method => method.Name == methodName
    && method.IsGenericMethodDefinition
    && method.GetGenericArguments().Length == 2
    && method.GetParameters().Length == 2)
    .MakeGenericMethod(typeof(T), type)
    .Invoke(null, new object { source, lambda });
    return (IOrderedQueryable<T>)result;
    }
    }









    share|improve this question



























      176












      176








      176


      25






      I'm getting the error:




      Extension methods must be defined in a non-generic static class




      On the line:



      public class LinqHelper


      Here is the helper class, based on Mark Gavells code. I'm really confused as to what this error means as I am sure it was working fine when I left it on Friday!



      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;
      using System.Linq.Expressions;
      using System.Reflection;

      /// <summary>
      /// Helper methods for link
      /// </summary>
      public class LinqHelper
      {
      public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string property)
      {
      return ApplyOrder<T>(source, property, "OrderBy");
      }
      public static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> source, string property)
      {
      return ApplyOrder<T>(source, property, "OrderByDescending");
      }
      public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source, string property)
      {
      return ApplyOrder<T>(source, property, "ThenBy");
      }
      public static IOrderedQueryable<T> ThenByDescending<T>(this IOrderedQueryable<T> source, string property)
      {
      return ApplyOrder<T>(source, property, "ThenByDescending");
      }
      static IOrderedQueryable<T> ApplyOrder<T>(IQueryable<T> source, string property, string methodName)
      {
      string props = property.Split('.');
      Type type = typeof(T);
      ParameterExpression arg = Expression.Parameter(type, "x");
      Expression expr = arg;
      foreach (string prop in props)
      {
      // use reflection (not ComponentModel) to mirror LINQ
      PropertyInfo pi = type.GetProperty(prop);
      expr = Expression.Property(expr, pi);
      type = pi.PropertyType;
      }
      Type delegateType = typeof(Func<,>).MakeGenericType(typeof(T), type);
      LambdaExpression lambda = Expression.Lambda(delegateType, expr, arg);

      object result = typeof(Queryable).GetMethods().Single(
      method => method.Name == methodName
      && method.IsGenericMethodDefinition
      && method.GetGenericArguments().Length == 2
      && method.GetParameters().Length == 2)
      .MakeGenericMethod(typeof(T), type)
      .Invoke(null, new object { source, lambda });
      return (IOrderedQueryable<T>)result;
      }
      }









      share|improve this question
















      I'm getting the error:




      Extension methods must be defined in a non-generic static class




      On the line:



      public class LinqHelper


      Here is the helper class, based on Mark Gavells code. I'm really confused as to what this error means as I am sure it was working fine when I left it on Friday!



      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;
      using System.Linq.Expressions;
      using System.Reflection;

      /// <summary>
      /// Helper methods for link
      /// </summary>
      public class LinqHelper
      {
      public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string property)
      {
      return ApplyOrder<T>(source, property, "OrderBy");
      }
      public static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> source, string property)
      {
      return ApplyOrder<T>(source, property, "OrderByDescending");
      }
      public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source, string property)
      {
      return ApplyOrder<T>(source, property, "ThenBy");
      }
      public static IOrderedQueryable<T> ThenByDescending<T>(this IOrderedQueryable<T> source, string property)
      {
      return ApplyOrder<T>(source, property, "ThenByDescending");
      }
      static IOrderedQueryable<T> ApplyOrder<T>(IQueryable<T> source, string property, string methodName)
      {
      string props = property.Split('.');
      Type type = typeof(T);
      ParameterExpression arg = Expression.Parameter(type, "x");
      Expression expr = arg;
      foreach (string prop in props)
      {
      // use reflection (not ComponentModel) to mirror LINQ
      PropertyInfo pi = type.GetProperty(prop);
      expr = Expression.Property(expr, pi);
      type = pi.PropertyType;
      }
      Type delegateType = typeof(Func<,>).MakeGenericType(typeof(T), type);
      LambdaExpression lambda = Expression.Lambda(delegateType, expr, arg);

      object result = typeof(Queryable).GetMethods().Single(
      method => method.Name == methodName
      && method.IsGenericMethodDefinition
      && method.GetGenericArguments().Length == 2
      && method.GetParameters().Length == 2)
      .MakeGenericMethod(typeof(T), type)
      .Invoke(null, new object { source, lambda });
      return (IOrderedQueryable<T>)result;
      }
      }






      c# .net linq extension-methods compiler-errors






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 23 '11 at 11:09









      abatishchev

      69.6k70263395




      69.6k70263395










      asked May 23 '11 at 11:00









      Tom GullenTom Gullen

      33.6k69240405




      33.6k69240405
























          8 Answers
          8






          active

          oldest

          votes


















          263














          change



          public class LinqHelper


          to



          public static class LinqHelper


          Following points need to be considered when creating an extension method:




          1. The class which defines an extension method must be non-generic, static and non-nested

          2. Every extension method must be a static method

          3. The first parameter of the extension method should use the this keyword.






          share|improve this answer





















          • 1





            Also add non-nested in the first point.

            – nawfal
            Jun 2 '13 at 7:16











          • If you have placed the class in App_Code then it is bound to have the static keyword in the class definition, but if you place it in any other folder then its fine to use it as normal class.

            – D.T.
            Sep 9 '14 at 6:58






          • 1





            In one case, I had used public static class IQueryable<T> where T : MyBaseClass which also generates this error. The where T : MyBaseClass phrase belongs on the individual methods without <T> on the static class.

            – Bron Davies
            Feb 11 '16 at 19:09













          • But what if the class is Partial? This solution did not work for me.

            – Fandango68
            Apr 24 '18 at 5:47



















          21














          Add keyword static to class declaration:



          // this is a non-generic static class
          public static class LinqHelper
          {
          }





          share|improve this answer































            16














            Change it to



            public static class LinqHelper





            share|improve this answer































              16














              Try changing



              public class LinqHelper


              to



               public static class LinqHelper





              share|improve this answer

































                9














                A work-around for people who are experiencing a bug like Nathan:



                The on-the-fly compiler seems to have a problem with this Extension Method error... adding static didn't help me either.



                I'd like to know what causes the bug?



                But the work-around is to write a new Extension class (not nested) even in same file and re-build.



                Figured that this thread is getting enough views that it's worth passing on (the limited) solution I found. Most people probably tried adding 'static' before google-ing for a solution! and I didn't see this work-around fix anywhere else.






                share|improve this answer


























                • I had the same issue. I just realized that I added a static function within the class and forgot to comment it out. So that made my class static, and hence was giving this error. Check if there are any static objects in your class.

                  – Mahesh
                  Aug 19 '16 at 21:39



















                2














                Extension method should be inside a static class.
                So please add your extension method inside a static class.



                so for example it should be like this



                public static class myclass
                {
                public static Byte ToByteArray(this Stream stream)
                {
                Int32 length = stream.Length > Int32.MaxValue ? Int32.MaxValue : Convert.ToInt32(stream.Length);
                Byte buffer = new Byte[length];
                stream.Read(buffer, 0, length);
                return buffer;
                }

                }





                share|improve this answer































                  2














                  if you do not intend to have static functions just get rid of the "this" keyword in the arguments.






                  share|improve this answer































                    0














                    Try changing it to static class and back. That might resolve visual studio complaining when it's a false positive.






                    share|improve this answer






















                      protected by Community Sep 10 '12 at 11:37



                      Thank you for your interest in this question.
                      Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                      Would you like to answer one of these unanswered questions instead?














                      8 Answers
                      8






                      active

                      oldest

                      votes








                      8 Answers
                      8






                      active

                      oldest

                      votes









                      active

                      oldest

                      votes






                      active

                      oldest

                      votes









                      263














                      change



                      public class LinqHelper


                      to



                      public static class LinqHelper


                      Following points need to be considered when creating an extension method:




                      1. The class which defines an extension method must be non-generic, static and non-nested

                      2. Every extension method must be a static method

                      3. The first parameter of the extension method should use the this keyword.






                      share|improve this answer





















                      • 1





                        Also add non-nested in the first point.

                        – nawfal
                        Jun 2 '13 at 7:16











                      • If you have placed the class in App_Code then it is bound to have the static keyword in the class definition, but if you place it in any other folder then its fine to use it as normal class.

                        – D.T.
                        Sep 9 '14 at 6:58






                      • 1





                        In one case, I had used public static class IQueryable<T> where T : MyBaseClass which also generates this error. The where T : MyBaseClass phrase belongs on the individual methods without <T> on the static class.

                        – Bron Davies
                        Feb 11 '16 at 19:09













                      • But what if the class is Partial? This solution did not work for me.

                        – Fandango68
                        Apr 24 '18 at 5:47
















                      263














                      change



                      public class LinqHelper


                      to



                      public static class LinqHelper


                      Following points need to be considered when creating an extension method:




                      1. The class which defines an extension method must be non-generic, static and non-nested

                      2. Every extension method must be a static method

                      3. The first parameter of the extension method should use the this keyword.






                      share|improve this answer





















                      • 1





                        Also add non-nested in the first point.

                        – nawfal
                        Jun 2 '13 at 7:16











                      • If you have placed the class in App_Code then it is bound to have the static keyword in the class definition, but if you place it in any other folder then its fine to use it as normal class.

                        – D.T.
                        Sep 9 '14 at 6:58






                      • 1





                        In one case, I had used public static class IQueryable<T> where T : MyBaseClass which also generates this error. The where T : MyBaseClass phrase belongs on the individual methods without <T> on the static class.

                        – Bron Davies
                        Feb 11 '16 at 19:09













                      • But what if the class is Partial? This solution did not work for me.

                        – Fandango68
                        Apr 24 '18 at 5:47














                      263












                      263








                      263







                      change



                      public class LinqHelper


                      to



                      public static class LinqHelper


                      Following points need to be considered when creating an extension method:




                      1. The class which defines an extension method must be non-generic, static and non-nested

                      2. Every extension method must be a static method

                      3. The first parameter of the extension method should use the this keyword.






                      share|improve this answer















                      change



                      public class LinqHelper


                      to



                      public static class LinqHelper


                      Following points need to be considered when creating an extension method:




                      1. The class which defines an extension method must be non-generic, static and non-nested

                      2. Every extension method must be a static method

                      3. The first parameter of the extension method should use the this keyword.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Jun 14 '16 at 8:33

























                      answered May 23 '11 at 11:03









                      cryptedcrypted

                      7,94623046




                      7,94623046








                      • 1





                        Also add non-nested in the first point.

                        – nawfal
                        Jun 2 '13 at 7:16











                      • If you have placed the class in App_Code then it is bound to have the static keyword in the class definition, but if you place it in any other folder then its fine to use it as normal class.

                        – D.T.
                        Sep 9 '14 at 6:58






                      • 1





                        In one case, I had used public static class IQueryable<T> where T : MyBaseClass which also generates this error. The where T : MyBaseClass phrase belongs on the individual methods without <T> on the static class.

                        – Bron Davies
                        Feb 11 '16 at 19:09













                      • But what if the class is Partial? This solution did not work for me.

                        – Fandango68
                        Apr 24 '18 at 5:47














                      • 1





                        Also add non-nested in the first point.

                        – nawfal
                        Jun 2 '13 at 7:16











                      • If you have placed the class in App_Code then it is bound to have the static keyword in the class definition, but if you place it in any other folder then its fine to use it as normal class.

                        – D.T.
                        Sep 9 '14 at 6:58






                      • 1





                        In one case, I had used public static class IQueryable<T> where T : MyBaseClass which also generates this error. The where T : MyBaseClass phrase belongs on the individual methods without <T> on the static class.

                        – Bron Davies
                        Feb 11 '16 at 19:09













                      • But what if the class is Partial? This solution did not work for me.

                        – Fandango68
                        Apr 24 '18 at 5:47








                      1




                      1





                      Also add non-nested in the first point.

                      – nawfal
                      Jun 2 '13 at 7:16





                      Also add non-nested in the first point.

                      – nawfal
                      Jun 2 '13 at 7:16













                      If you have placed the class in App_Code then it is bound to have the static keyword in the class definition, but if you place it in any other folder then its fine to use it as normal class.

                      – D.T.
                      Sep 9 '14 at 6:58





                      If you have placed the class in App_Code then it is bound to have the static keyword in the class definition, but if you place it in any other folder then its fine to use it as normal class.

                      – D.T.
                      Sep 9 '14 at 6:58




                      1




                      1





                      In one case, I had used public static class IQueryable<T> where T : MyBaseClass which also generates this error. The where T : MyBaseClass phrase belongs on the individual methods without <T> on the static class.

                      – Bron Davies
                      Feb 11 '16 at 19:09







                      In one case, I had used public static class IQueryable<T> where T : MyBaseClass which also generates this error. The where T : MyBaseClass phrase belongs on the individual methods without <T> on the static class.

                      – Bron Davies
                      Feb 11 '16 at 19:09















                      But what if the class is Partial? This solution did not work for me.

                      – Fandango68
                      Apr 24 '18 at 5:47





                      But what if the class is Partial? This solution did not work for me.

                      – Fandango68
                      Apr 24 '18 at 5:47













                      21














                      Add keyword static to class declaration:



                      // this is a non-generic static class
                      public static class LinqHelper
                      {
                      }





                      share|improve this answer




























                        21














                        Add keyword static to class declaration:



                        // this is a non-generic static class
                        public static class LinqHelper
                        {
                        }





                        share|improve this answer


























                          21












                          21








                          21







                          Add keyword static to class declaration:



                          // this is a non-generic static class
                          public static class LinqHelper
                          {
                          }





                          share|improve this answer













                          Add keyword static to class declaration:



                          // this is a non-generic static class
                          public static class LinqHelper
                          {
                          }






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered May 23 '11 at 11:02









                          abatishchevabatishchev

                          69.6k70263395




                          69.6k70263395























                              16














                              Change it to



                              public static class LinqHelper





                              share|improve this answer




























                                16














                                Change it to



                                public static class LinqHelper





                                share|improve this answer


























                                  16












                                  16








                                  16







                                  Change it to



                                  public static class LinqHelper





                                  share|improve this answer













                                  Change it to



                                  public static class LinqHelper






                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered May 23 '11 at 11:02









                                  RikRik

                                  22.1k114561




                                  22.1k114561























                                      16














                                      Try changing



                                      public class LinqHelper


                                      to



                                       public static class LinqHelper





                                      share|improve this answer






























                                        16














                                        Try changing



                                        public class LinqHelper


                                        to



                                         public static class LinqHelper





                                        share|improve this answer




























                                          16












                                          16








                                          16







                                          Try changing



                                          public class LinqHelper


                                          to



                                           public static class LinqHelper





                                          share|improve this answer















                                          Try changing



                                          public class LinqHelper


                                          to



                                           public static class LinqHelper






                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          edited Nov 1 '16 at 2:27









                                          Drew

                                          22.2k83260




                                          22.2k83260










                                          answered May 23 '11 at 11:03









                                          NathanNathan

                                          5,05822754




                                          5,05822754























                                              9














                                              A work-around for people who are experiencing a bug like Nathan:



                                              The on-the-fly compiler seems to have a problem with this Extension Method error... adding static didn't help me either.



                                              I'd like to know what causes the bug?



                                              But the work-around is to write a new Extension class (not nested) even in same file and re-build.



                                              Figured that this thread is getting enough views that it's worth passing on (the limited) solution I found. Most people probably tried adding 'static' before google-ing for a solution! and I didn't see this work-around fix anywhere else.






                                              share|improve this answer


























                                              • I had the same issue. I just realized that I added a static function within the class and forgot to comment it out. So that made my class static, and hence was giving this error. Check if there are any static objects in your class.

                                                – Mahesh
                                                Aug 19 '16 at 21:39
















                                              9














                                              A work-around for people who are experiencing a bug like Nathan:



                                              The on-the-fly compiler seems to have a problem with this Extension Method error... adding static didn't help me either.



                                              I'd like to know what causes the bug?



                                              But the work-around is to write a new Extension class (not nested) even in same file and re-build.



                                              Figured that this thread is getting enough views that it's worth passing on (the limited) solution I found. Most people probably tried adding 'static' before google-ing for a solution! and I didn't see this work-around fix anywhere else.






                                              share|improve this answer


























                                              • I had the same issue. I just realized that I added a static function within the class and forgot to comment it out. So that made my class static, and hence was giving this error. Check if there are any static objects in your class.

                                                – Mahesh
                                                Aug 19 '16 at 21:39














                                              9












                                              9








                                              9







                                              A work-around for people who are experiencing a bug like Nathan:



                                              The on-the-fly compiler seems to have a problem with this Extension Method error... adding static didn't help me either.



                                              I'd like to know what causes the bug?



                                              But the work-around is to write a new Extension class (not nested) even in same file and re-build.



                                              Figured that this thread is getting enough views that it's worth passing on (the limited) solution I found. Most people probably tried adding 'static' before google-ing for a solution! and I didn't see this work-around fix anywhere else.






                                              share|improve this answer















                                              A work-around for people who are experiencing a bug like Nathan:



                                              The on-the-fly compiler seems to have a problem with this Extension Method error... adding static didn't help me either.



                                              I'd like to know what causes the bug?



                                              But the work-around is to write a new Extension class (not nested) even in same file and re-build.



                                              Figured that this thread is getting enough views that it's worth passing on (the limited) solution I found. Most people probably tried adding 'static' before google-ing for a solution! and I didn't see this work-around fix anywhere else.







                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited Feb 21 '14 at 10:57

























                                              answered Feb 20 '14 at 23:16









                                              Stephan LuisStephan Luis

                                              4091615




                                              4091615













                                              • I had the same issue. I just realized that I added a static function within the class and forgot to comment it out. So that made my class static, and hence was giving this error. Check if there are any static objects in your class.

                                                – Mahesh
                                                Aug 19 '16 at 21:39



















                                              • I had the same issue. I just realized that I added a static function within the class and forgot to comment it out. So that made my class static, and hence was giving this error. Check if there are any static objects in your class.

                                                – Mahesh
                                                Aug 19 '16 at 21:39

















                                              I had the same issue. I just realized that I added a static function within the class and forgot to comment it out. So that made my class static, and hence was giving this error. Check if there are any static objects in your class.

                                              – Mahesh
                                              Aug 19 '16 at 21:39





                                              I had the same issue. I just realized that I added a static function within the class and forgot to comment it out. So that made my class static, and hence was giving this error. Check if there are any static objects in your class.

                                              – Mahesh
                                              Aug 19 '16 at 21:39











                                              2














                                              Extension method should be inside a static class.
                                              So please add your extension method inside a static class.



                                              so for example it should be like this



                                              public static class myclass
                                              {
                                              public static Byte ToByteArray(this Stream stream)
                                              {
                                              Int32 length = stream.Length > Int32.MaxValue ? Int32.MaxValue : Convert.ToInt32(stream.Length);
                                              Byte buffer = new Byte[length];
                                              stream.Read(buffer, 0, length);
                                              return buffer;
                                              }

                                              }





                                              share|improve this answer




























                                                2














                                                Extension method should be inside a static class.
                                                So please add your extension method inside a static class.



                                                so for example it should be like this



                                                public static class myclass
                                                {
                                                public static Byte ToByteArray(this Stream stream)
                                                {
                                                Int32 length = stream.Length > Int32.MaxValue ? Int32.MaxValue : Convert.ToInt32(stream.Length);
                                                Byte buffer = new Byte[length];
                                                stream.Read(buffer, 0, length);
                                                return buffer;
                                                }

                                                }





                                                share|improve this answer


























                                                  2












                                                  2








                                                  2







                                                  Extension method should be inside a static class.
                                                  So please add your extension method inside a static class.



                                                  so for example it should be like this



                                                  public static class myclass
                                                  {
                                                  public static Byte ToByteArray(this Stream stream)
                                                  {
                                                  Int32 length = stream.Length > Int32.MaxValue ? Int32.MaxValue : Convert.ToInt32(stream.Length);
                                                  Byte buffer = new Byte[length];
                                                  stream.Read(buffer, 0, length);
                                                  return buffer;
                                                  }

                                                  }





                                                  share|improve this answer













                                                  Extension method should be inside a static class.
                                                  So please add your extension method inside a static class.



                                                  so for example it should be like this



                                                  public static class myclass
                                                  {
                                                  public static Byte ToByteArray(this Stream stream)
                                                  {
                                                  Int32 length = stream.Length > Int32.MaxValue ? Int32.MaxValue : Convert.ToInt32(stream.Length);
                                                  Byte buffer = new Byte[length];
                                                  stream.Read(buffer, 0, length);
                                                  return buffer;
                                                  }

                                                  }






                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Apr 12 '16 at 8:06









                                                  Debendra DashDebendra Dash

                                                  2,2151921




                                                  2,2151921























                                                      2














                                                      if you do not intend to have static functions just get rid of the "this" keyword in the arguments.






                                                      share|improve this answer




























                                                        2














                                                        if you do not intend to have static functions just get rid of the "this" keyword in the arguments.






                                                        share|improve this answer


























                                                          2












                                                          2








                                                          2







                                                          if you do not intend to have static functions just get rid of the "this" keyword in the arguments.






                                                          share|improve this answer













                                                          if you do not intend to have static functions just get rid of the "this" keyword in the arguments.







                                                          share|improve this answer












                                                          share|improve this answer



                                                          share|improve this answer










                                                          answered Sep 7 '18 at 6:28









                                                          Rohan BhosaleRohan Bhosale

                                                          311




                                                          311























                                                              0














                                                              Try changing it to static class and back. That might resolve visual studio complaining when it's a false positive.






                                                              share|improve this answer




























                                                                0














                                                                Try changing it to static class and back. That might resolve visual studio complaining when it's a false positive.






                                                                share|improve this answer


























                                                                  0












                                                                  0








                                                                  0







                                                                  Try changing it to static class and back. That might resolve visual studio complaining when it's a false positive.






                                                                  share|improve this answer













                                                                  Try changing it to static class and back. That might resolve visual studio complaining when it's a false positive.







                                                                  share|improve this answer












                                                                  share|improve this answer



                                                                  share|improve this answer










                                                                  answered Feb 2 '18 at 18:51









                                                                  viscvisc

                                                                  1,00811433




                                                                  1,00811433

















                                                                      protected by Community Sep 10 '12 at 11:37



                                                                      Thank you for your interest in this question.
                                                                      Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                                                                      Would you like to answer one of these unanswered questions instead?



                                                                      Popular posts from this blog

                                                                      Contact image not getting when fetch all contact list from iPhone by CNContact

                                                                      count number of partitions of a set with n elements into k subsets

                                                                      A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks