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 6735549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:01:32+00:00 2026-05-26T11:01:32+00:00

What I’m doing now: void Main() { var command1 = new PersistenceCommand(new MyIntBO()); var

  • 0

What I’m doing now:

void Main()
{
    var command1 = new PersistenceCommand(new MyIntBO());
    var command2 = new PersistenceCommand(new MyGuidBO());
    var command3 = new PersistenceCommand(new PersistentBO());

    Console.WriteLine(command1.ToString());
    Console.WriteLine(command2.ToString());
    Console.WriteLine(command3.ToString());
}

public class PersistenceCommand
{
    public PersistenceCommand(PersistentBO businessObject)
    {
        _businessObject = businessObject;
    }

    public override string ToString()
    {
        string result = _businessObject.GetType().Name;

        var keyed = _businessObject as IPrimaryKeyed<int>;

        if (keyed != null)
        {
            result += " " + keyed.Id.ToString();
        }

        return result;
    }

    private readonly PersistentBO _businessObject;
}

public interface IPrimaryKeyed<out TKey>
{
    TKey Id { get; }
}

public class PersistentBO {}

public class MyIntBO : PersistentBO, IPrimaryKeyed<int>
{
    public int Id { get { return 1008; } }
}

public class MyGuidBO : PersistentBO, IPrimaryKeyed<Guid>
{
    public Guid Id
    {
        get
        {
            return new Guid("6135d49b-81bb-43d4-9b74-dd84c2d3cc29");
        }
    }
}

This prints:

MyIntBO 1008
MyGuidBO
PersistentBO

I’d like it to print:

MyIntBO 1008
MyGuidBO 6135d49b-81bb-43d4-9b74-dd84c2d3cc29
PersistentBO

What’s the most elegant way to do that?

I want to support all types of keys – int, long, Guid, etc. – so I’d rather not do multiple casts. Note that not every business object implements that interface (some do not have a single primary key).

I realize I could use reflection and try to access the Id property. I was wondering if there’s a better solution.

Clarification: To address @Acaz Souza and @Petar Ivanov’s answers, we have dozens of classes scattered over multiple assemblies that already implement IPrimaryKeyed<T>. I do not want to break all of them by extending the interface contract. If I were designing this from scratch, their solutions would work.

  • 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-26T11:01:32+00:00Added an answer on May 26, 2026 at 11:01 am

    Using reflection doesn’t seem like a bad way to go here.

    ToString method:

    // for getting the Id prop
    var identProp = _businessObject.GetType().GetProperty("Id");
    string result = _businessObject.GetType().Name;
    
    if (identProp != null)
    {
        result += " " + identProp.GetValue(_businessObject, null).ToString();
    }  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I am trying to understand how to use SyndicationItem to display feed which is
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.