Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 775173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:14:53+00:00 2026-05-14T19:14:53+00:00

In an extension method how do a create an object based on the implementation

  • 0

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 relationship

   public static class TopologyExtns
    {

        public static void AddNode<T>(this ITopology<T> topIf, INode<T> node)
        {
            topIf.Nodes.Add(node.Key, node);
        }

        public static INode<T> FindNode<T>(this ITopology<T> topIf, T searchKey)
        {
            return topIf.Nodes[searchKey];
        }

        public static bool AddRelationship<T>(this ITopology<T> topIf, INode<T> parentNode, INode<T> childNode)
        {
            var rel = new RelationshipImp();  // ** How do I create an object from teh implementation 
            // Add nodes to Relationship
            // Add relationships to Nodes
        }
    }


    public interface ITopology<T>
    {
        //List<INode> Nodes { get; set; }
        Dictionary<T, INode<T> > Nodes { get; set; }
    }

    public interface INode<T> 
    {
        // Properties
        List<IRelationship<T>> Relationships { get; set; }
        T Key { get; }
    }

    public interface IRelationship<T>
    {
        // Parameters
        INode<T> Parent { get; set; }
        INode<T> Child { get; set; }
    }


namespace TopologyLibrary_Client
{

    class RelationshipsImp : IRelationship<string>
    {
        public INode<string> Parent { get; set; }
        public INode<string> Child { get; set; }
    }
}

    public class TopologyImp<T> : ITopology<T>
    {
        public Dictionary<T, INode<T>> Nodes { get; set; }
        public TopologyImp()
        {
            Nodes = new Dictionary<T, INode<T>>();
        }

    }

thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T19:14:54+00:00Added an answer on May 14, 2026 at 7:14 pm

    You could add an extra type parameter representing the relationship implementation class, specifying the constraints that it must be an IRelationship and have a parameterless constructor:

    public static bool AddRelationship<T,R>(this ITopology<T> topIf, INode<T> parentNode, INode<T> childNode) 
        where R : IRelationship, new() {
        var rel = new R();
        // ...
    }
    

    Then you should call it like this to specify the IRelationship concrete type (the ‘R’ type parameter):

    topology.AddRelationship<string, RelationshipImp>(parentNode, childNode);
    

    EDIT: alternative approach, without extension method: you define (and later instantiate) a RelationshipFactory class:

    class RelationshipFactory<R> 
        where R : IRelationship, new(){
        // no longer an extension method:
        public static bool AddRelationship<T>(ITopology<T> topIf, INode<T> parentNode, INode<T> childNode) {
            var rel = new R();
            // ...
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 513k
  • Answers 513k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You added the checkbox to the cell, not the row:… May 16, 2026 at 5:47 pm
  • Editorial Team
    Editorial Team added an answer Jquery or similiar would be the most elegant imo. something… May 16, 2026 at 5:47 pm
  • Editorial Team
    Editorial Team added an answer I found that it works if you change the webview… May 16, 2026 at 5:47 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Is there a way to create a mock object based on an already existing
I'm trying to create an Extension Method for MVC's htmlHelper. The purpose is to
Possible Duplicate: Why is there not a ForEach extension method on the IEnumerable interface?
How would you create an extension method which enables me to do the following
I've made the following extension method ... public static class ObjectExtensions { public static
So I want to be able to add/remove class methods at runtime. Before you
I have this little bit of code: using System; using System.Web.Mvc; public class SecureFilter
I'm getting the error: 'Namespace.A' does not contain a definition for 'MyObjectInterface' and no
I've been playing with collections and threading and came across the nifty extension methods
The end goal for this post is to override the ToString() method of a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.