Hi,
Is it possible to apply an extension method to an interface? (C# question)
That is for example to achieve the following:
create an ITopology interface
create an extension method for this interface (e.g. public static int CountNodes(this ITopology topologyIf) )
then when creating a class (e.g. MyGraph) which implements ITopology,...
Hi,
Using extension methods for C#, how can include an AddNode method (see below) in the ExtensionMethods class for reuse, as opposed to having to put it in the implementation class itself (where I couldn't reuse it). The AddNode extension method needs to be able to access the List parameter in the implementation.
I've tried the bel...
Hi
How could I make this code more generic in the sense that the Dictionary key could be a different type, depending on what the user of the library wanted to implement? For example someone might what to use the extension methods/interfaces in a case where there "unique key" so to speak for Node is actually an "int" not a "string" for e...
So here's the thing: I've got an app that I'm testing that uses LINQ to Entities (EF4/.NET4). The app binds to an implementation of the Contains method that ignores nulls and, due to the way the database is configured, ignores case. That works great.
However, when I call into the same methods from my unit tests, I'm passing in a fake co...
Hi,
In an extension method how do a create an object based on the implementation class. So in the code below I wanted to add an "AddRelationship" extension method, however I'm not sure how within the extension method I can create an Relationship object? i.e. don't want to tie the extension method to this particular implementation of r...
Hi,
Is there a rule for knowing when one has to pass the generic type parameters in the client code when calling an extension method?
So for example in the Program class why can I (a) not pass type parameters for top.AddNode(node), but where as later for the (b) top.AddRelationship line I have to pass them?
class Program
{
stati...
To convert between some LINQ to SQL objects and DTOs we have created explicit cast operators on the DTOs. That way we can do the following:
DTOType MyDTO = (LinqToSQLType)MyLinq2SQLObj;
This works well.
However when you try to cast using the LINQ .Cast() extension method it trows an invalid cast exception saying cannot cast type Lin...
Suppose I have a Dictionary<String,String>, and I want to produce a string representation of it. The "stone tools" way of doing it would be:
private static string DictionaryToString(Dictionary<String,String> hash)
{
var list = new List<String> ();
foreach (var kvp in hash)
{
list.Add(kvp.Key + ":" + kvp.Value);
...
I'm trying to write an extension method that I can use to copy values from one object property to another object of a different type, as long as the property names and types match exactly.
This is what I have:
public static T CopyFrom<T>(this T toObject, object fromObject)
{
var fromObjectType = fromObject.GetType();
...
I'm looking for a concise way to process every (unordered) pair of elements in a sequence in .NET.
I know I can do it with nested for loops, but I was looking for something a little more readable.
I was imagining something like a modified Any() extension method:
IEnumerable<Transaction> transactions = ...
if (transactions.AnyPair( (fir...
I got some static classes with extension methods which add 'business logic' to entities using the repository pattern.
Now sometimes i need to create a new IRepository in these extension functions.
I'm currently working around it by accessing my Ninject kernel through the object I'm extending, but it's really ugly:
public static IEnume...
I want to have an extension method for XElement/XAttribute that allows me to apply a "ValueOrDefault" logic - perhaps with various slightly different implementations: ValueOrNull, ValueOrDefault, NumericValueOrDefault (which validates if the value is numeric), but I want to constrain these methods so that they can only work with ValueTyp...
I am writing a dirty little extension method for HtmlHelper so that I can say something like HtmlHelper.WysiwygFor(lambda) and display the CKEditor.
I have this working currently but it seems a bit more cumbersome than I would prefer. I am hoping that there is a more straight forward way of doing this.
Here is what I have so far.
pub...
I have been using these common EntityObjectFilters as a "pipes and filters" way to query from a collection a particular item with an ID:
public static class EntityObjectFilters
{
public static T WithID<T>(this IQueryable<T> qry,
int ID) where T : IEntityObject
{
return qry.SingleOrDefault<T>(item => item.ID == ID)...
Hi there
I have a class.
public class MedicalRequest
{
private int id
private IList<MedicalDays> Days
private string MedicalUser
...
}
and another
public class MedicalDays
{
private int id;
private DateTime? day
private MedicalRequest request
...
}
I'm using nhibernate to return a list of all t...
what is the difference between returning iqueryable vs ienumerable.
IQueryable<Customer> custs = from c in db.Customers
where c.City == "<City>"
select c;
IEnumerable<Customer> custs = from c in db.Customers
where c.City == "<City>"
select c;
Will both be deferred execution? When should one be preferred over the other?
...
Hi,
I have:
Main Program Class - uses Library A
Library A - has partial classes which mix in methods from Library B
Library B - mix in methods & interfaces
Why would I need a using statement to LibaryB just to get their extension methods working in the main class? That is given that it's Library B that defines the classes that wil...
OK, I've probably misunderstood something here but, as far as I can see ...
An extension method has to be contained in a module, not a class
You can't make methods in modules Static/Shared
Therefore you can't use an extension method on a class without instantiating it.
In other words you can't make an extension method on String calle...
Can anyone explain why the following happens? And how to resolve, Visual Studio 2010 and MVC2
<%= Html.ActionLink("Add New Option", "AddOption", "Product", new { @class = "lighbox" }, null)%>
Results in
/Product/AddOption?class=lightbox
<%= Html.ActionLink("Add New Option", "AddOption", "Product", new { @class = "lighbox" })%>
...
I have a simple extension method that I would like to use to add an item to an array of items.
public static T[] addElement<T>(this T[] array, T elementToAdd)
{
var list = new List<T>(array) { elementToAdd };
return list.ToArray();
}
this works ok, but when I use it, I am having to set the array equal to the return value. I se...