Extension methods must be defined in a non-generic static class
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
add a comment |
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
add a comment |
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
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
c# .net linq extension-methods compiler-errors
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
add a comment |
add a comment |
8 Answers
8
active
oldest
votes
change
public class LinqHelper
to
public static class LinqHelper
Following points need to be considered when creating an extension method:
- The class which defines an extension method must be
non-generic
,static
andnon-nested
- Every extension method must be a
static
method - The first parameter of the extension method should use the
this
keyword.
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 usedpublic static class IQueryable<T> where T : MyBaseClass
which also generates this error. Thewhere 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
add a comment |
Add keyword static
to class declaration:
// this is a non-generic static class
public static class LinqHelper
{
}
add a comment |
Change it to
public static class LinqHelper
add a comment |
Try changing
public class LinqHelper
to
public static class LinqHelper
add a comment |
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.
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
add a comment |
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;
}
}
add a comment |
if you do not intend to have static functions just get rid of the "this" keyword in the arguments.
add a comment |
Try changing it to static class and back. That might resolve visual studio complaining when it's a false positive.
add a comment |
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
change
public class LinqHelper
to
public static class LinqHelper
Following points need to be considered when creating an extension method:
- The class which defines an extension method must be
non-generic
,static
andnon-nested
- Every extension method must be a
static
method - The first parameter of the extension method should use the
this
keyword.
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 usedpublic static class IQueryable<T> where T : MyBaseClass
which also generates this error. Thewhere 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
add a comment |
change
public class LinqHelper
to
public static class LinqHelper
Following points need to be considered when creating an extension method:
- The class which defines an extension method must be
non-generic
,static
andnon-nested
- Every extension method must be a
static
method - The first parameter of the extension method should use the
this
keyword.
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 usedpublic static class IQueryable<T> where T : MyBaseClass
which also generates this error. Thewhere 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
add a comment |
change
public class LinqHelper
to
public static class LinqHelper
Following points need to be considered when creating an extension method:
- The class which defines an extension method must be
non-generic
,static
andnon-nested
- Every extension method must be a
static
method - The first parameter of the extension method should use the
this
keyword.
change
public class LinqHelper
to
public static class LinqHelper
Following points need to be considered when creating an extension method:
- The class which defines an extension method must be
non-generic
,static
andnon-nested
- Every extension method must be a
static
method - The first parameter of the extension method should use the
this
keyword.
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 usedpublic static class IQueryable<T> where T : MyBaseClass
which also generates this error. Thewhere 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
add a comment |
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 usedpublic static class IQueryable<T> where T : MyBaseClass
which also generates this error. Thewhere 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
add a comment |
Add keyword static
to class declaration:
// this is a non-generic static class
public static class LinqHelper
{
}
add a comment |
Add keyword static
to class declaration:
// this is a non-generic static class
public static class LinqHelper
{
}
add a comment |
Add keyword static
to class declaration:
// this is a non-generic static class
public static class LinqHelper
{
}
Add keyword static
to class declaration:
// this is a non-generic static class
public static class LinqHelper
{
}
answered May 23 '11 at 11:02
abatishchevabatishchev
69.6k70263395
69.6k70263395
add a comment |
add a comment |
Change it to
public static class LinqHelper
add a comment |
Change it to
public static class LinqHelper
add a comment |
Change it to
public static class LinqHelper
Change it to
public static class LinqHelper
answered May 23 '11 at 11:02
RikRik
22.1k114561
22.1k114561
add a comment |
add a comment |
Try changing
public class LinqHelper
to
public static class LinqHelper
add a comment |
Try changing
public class LinqHelper
to
public static class LinqHelper
add a comment |
Try changing
public class LinqHelper
to
public static class LinqHelper
Try changing
public class LinqHelper
to
public static class LinqHelper
edited Nov 1 '16 at 2:27
Drew
22.2k83260
22.2k83260
answered May 23 '11 at 11:03
NathanNathan
5,05822754
5,05822754
add a comment |
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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;
}
}
add a comment |
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;
}
}
add a comment |
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;
}
}
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;
}
}
answered Apr 12 '16 at 8:06
Debendra DashDebendra Dash
2,2151921
2,2151921
add a comment |
add a comment |
if you do not intend to have static functions just get rid of the "this" keyword in the arguments.
add a comment |
if you do not intend to have static functions just get rid of the "this" keyword in the arguments.
add a comment |
if you do not intend to have static functions just get rid of the "this" keyword in the arguments.
if you do not intend to have static functions just get rid of the "this" keyword in the arguments.
answered Sep 7 '18 at 6:28
Rohan BhosaleRohan Bhosale
311
311
add a comment |
add a comment |
Try changing it to static class and back. That might resolve visual studio complaining when it's a false positive.
add a comment |
Try changing it to static class and back. That might resolve visual studio complaining when it's a false positive.
add a comment |
Try changing it to static class and back. That might resolve visual studio complaining when it's a false positive.
Try changing it to static class and back. That might resolve visual studio complaining when it's a false positive.
answered Feb 2 '18 at 18:51
viscvisc
1,00811433
1,00811433
add a comment |
add a comment |
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?