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

  • SEARCH
  • Home
  • 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 8222775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:23:33+00:00 2026-06-07T14:23:33+00:00

I have the following class hierarchy. public abstract class ResourceBase { } public abstract

  • 0

I have the following class hierarchy.

public abstract class ResourceBase { }

public abstract class WebResourceBase : ResourceBase  {
  public ResourceBase LocalPath { get; set; }
  public ResourceBase FtpPath { get; set; }
}

public class JavaScript : WebResourceBase { }

What I would like to do is have a declaration like so.

new JavaScript() {
  LocalPath = "/path/goes/here/1.js",
  FtpPath = "ftp://path/goes/here/1.js"
}

The obvious answer here would be to use implicit operators but the problem is that I want to assign a derived type to those properties which is the same as the declared type so LocalPath and FtpPath would be of type JavaScript.

I’d like my solution to be more flexible than what I have at the moment. This code just makes my skin crawl. Was hoping there was a way using reflection and I have tried looking for information using the StackTrace class but no luck. Any help would be appreciated. Thanks.

public abstract class ResourceBase { 
  public static implicit operator ResourceBase(string path) {
            if (path.EndsWith(".js"))
                return new JavaScript(path);
            // etc...
  }
}
  • 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-06-07T14:23:35+00:00Added an answer on June 7, 2026 at 2:23 pm

    This assumes that WebResourceBase is actually meant to inherit ResourceBase.

    You won’t be able to make the implicit operator look much nicer, unfortunately – generics won’t work here.


    Alternative: Generics to constrain ResourceBase

    Now that I’ve re-read it and understand what you’re after, one option is to amend your classes to include a generic parameter referencing derived classes (sort of like a self-reference):

    public abstract class ResourceBase 
    { }
    
    public abstract class WebResourceBase<T> : ResourceBase
        where T : WebResourceBase<T>
    {
        public T LocalPath { get; set; }
        public T FtpPath { get; set; }
    }
    
    public class JavaScript : WebResourceBase<JavaScript> 
    { 
    }
    

    Then you will see that in JavaScript, the properties LocalPath and FtpPath are now of type JavaScript also.

    Now your assignment will only accept JavaScript types:

    new JavaScript() 
    {
        LocalPath = new JavaScript("/path/goes/here/1.js"),
        FtpPath = new JavaScript("ftp://path/goes/here/1.js")
    }
    

    The benefit of this approach is it will constrain the base properties to be of the current type or more derived, not less derived.


    Alternative: Explicit parsing instead of implicit operator

    If you need to leave the LocalPath and FtpPath variables as ResourceBase, or otherwise cannot use generics here, your implicit operator will start to get confusing. Better to provide something explicit like a static method:

    new JavaScript() 
    {
        LocalPath = JavaScript.Parse("/path/goes/here/1.js"),
        FtpPath = JavaScript.Parse("ftp://path/goes/here/1.js")
    }
    
    class JavaScript
    {
        public static ResourceBase Parse(string s)
        {
            if (path.EndsWith(".js"))
                return new JavaScript(path);
    
            throw new Exception();
        }
    }
    

    Alternative: Class hierarchy parsing instead of implicit operator

    Bake the concept of consuming strings into the types via constructors and make the properties public read-only:

    public abstract class ResourceBase
    { }
    
    public abstract class WebResourceBase
    {
        public ResourceBase LocalPath { get; private set; }
        public ResourceBase FtpPath { get; private set; }
    
        protected abstract ResourceBase ParseLocalPath(string s);
        protected abstract ResourceBase ParseFtpPath(string s);
    }
    
    public class JavaScript : WebResourceBase<JavaScript> 
    { 
        protected override ResourceBase ParseLocalPath(string s)
        {
            // etc.
        } 
        protected override ResourceBase ParseFtpPath(string s)
        {
            // etc.
        } 
    }
    

    To be honest, most of this seems a little overkill just to get two properties set as a particular type from a string, you have loads of options – even the implicit operator will work.

    Pick the one that is easiest to understand. Operator overloading tends to be somewhat hidden unless you go digging for it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following class hierarchy [BsonKnownTypes(typeof(MoveCommand))] public abstract class Command : ICommand {
I have something like following: public interface A { .... } public abstract class
i have the following (partial) hierarchy: @MappedSuperclass public abstract class PersistentEntity { @Id @GeneratedValue(generator=system-uuid)
I have a class hierarchy like this public abstract class CalendarEventBase{} public class TrainingEvent
I have the following class hierarchy class Test { public string Name { get;
If I have following type hierarchy: abstract class TicketBase { public DateTime PublishedDate {
I have the following class hierarchy public abstract BaseClass : System.Web.UI.UserControl { public virtual
I have the following class hierarchy: public abstract class BaseData { //some properties }
I have the following class hierarchy: public abstract class NumberRangeParameter<T extends Comparable<T>> extends ComplexParameter{
I have following classes: public abstract class CustomerBase { public long CustomerNumber { get;

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.