C
C#2y ago
gumbarros

❔ What is the difference between public static and public static virtual in a interface?

This is a interface for dynamic plugins loaded from a AssemblyLoadContext.
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace JJIntegration.PluginContracts;

public interface IScheduleTask
{
/// more code above...

/// <summary>
/// Name of the task that will appear to the user
/// </summary>
string TaskName { get; }



/// <summary>
/// Add the dependencies from this ScheduleTask to the IServiceCollection of the application
/// </summary>


// My friends, what is the difference here if I add a virtual keyword? This method is meant to be optionally overriden.
public static IServiceCollection AddServices(IServiceCollection services, IConfiguration configuration)
{
return services;
}

/// more code below...
}
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace JJIntegration.PluginContracts;

public interface IScheduleTask
{
/// more code above...

/// <summary>
/// Name of the task that will appear to the user
/// </summary>
string TaskName { get; }



/// <summary>
/// Add the dependencies from this ScheduleTask to the IServiceCollection of the application
/// </summary>


// My friends, what is the difference here if I add a virtual keyword? This method is meant to be optionally overriden.
public static IServiceCollection AddServices(IServiceCollection services, IConfiguration configuration)
{
return services;
}

/// more code below...
}
What is the difference between public static virtual and public static in a interface? When I use public static virtual, the method cannot be loaded by reflection.
3 Replies
gumbarros
gumbarrosOP2y ago
The main application also references Microsoft.Extensions.DependencyInjection.Abstractions, is this a problem?
333fred
333fred2y ago
Explore static virtual members in interfaces
This advanced tutorial demonstrates scenarios for operators and other static members in interfaces.
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server