logo
down
shadow

How to tell a class which objects it should create? Type vs. object confusion :(


How to tell a class which objects it should create? Type vs. object confusion :(

Content Index :

How to tell a class which objects it should create? Type vs. object confusion :(
Tag : chash , By : 03B
Date : November 28 2020, 08:01 AM

I wish this helpful for you I have a class (SetOfRules) that wants to handle rules (somehow). Task: create a new instance of this class (that's easy), and later add rules (defined in a class Rules and derived classes) to the instance. , It looks like you want a singleton class with contract:
public interface IRule
{
    void Transform(object o);
}
public abstract class SingletonBase<T> where T : SingletonBase<T>, new()
{
    public static T Instance { get; } = new T();
}
public class SpecificRule1 : SingletonBase<SpecificRule1>, IRule
{
    public void Transform(object o) => throw new NotImplementedException();
}
public class SpecificRule2 : SingletonBase<SpecificRule2>, IRule
{
    public void Transform(object o) => throw new NotImplementedException();
}
public class RuleSet : List<IRule>
{
    public void Add<TRule>() where TRule : SingletonBase<TRule>, IRule, new()
    {
        this.Add(SingletonBase<TRule>.Instance);
    }
}
var rules = new RuleSet
{
    SpecificRule1.Instance,
    SpecificRule2.Instance,
};

rules = new RuleSet();
rules.Add(SpecificRule1.Instance);
rules.Add<SpecificRule2>();

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

How to get type of an object and then create array of objects with this type


Tag : chash , By : silvervino
Date : March 29 2020, 07:55 AM
will be helpful for those in need , For the simple approach :
if(first is baker)
{
// Create array of bakers
}
void Round<T>(T first, T second,...)
{
// Create the list of the type
new List<T>();
Array.CreateInstance(first.GetType(), length);

Create an object from another objects type


Tag : vb.net , By : Bimal Poudel
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , Dim TestObject As TestObjectType.GetType will look for a type named GetType in the namespace TestObjectType.
Dim TestObject = Activator.CreateInstance(TestObjectType.GetType())
Dim listType = GetType(List(Of )).MakeGenericType(TestObjectType.GetType())
Dim list = Activator.CreateInstance(listType)
Dim TestObject = CreateNew(TestObjectType)
Dim AuthorList = CreateNewList(TestObjectType)

...

Function CreateNew(Of T As New)(obj As T) As T 
    Return New T()
End Function

Function CreateNewList(Of T)(obj As T) As List(Of T)
    Return New List(Of T)
End Function

Type confusion: A type is class, a variable and an object?


Tag : chash , By : msugar
Date : March 29 2020, 07:55 AM
will be helpful for those in need I'm currently working through the Pluralsight C# 5.0 course, and I'm relatively new to programming. , GradeStatistics is a class, from this declaration:
public class GradeStatistics
GradeStatistics stats = new GradeStatistics();
public GradeStatistics ComputeStatistics()

Create a new object class or write a method which converts sub-class objects? or something else? Performance NOT Prefere


Tag : java , By : Janne Laine
Date : March 29 2020, 07:55 AM
wish of those help Too many subclasses
Neither of these approaches is very good, as both of them have more subclasses than necessary. Why does EightsCard extend Card? What if you wanted to implement some other card game? (There are quite a few, you know...) Would you make one subclass for each card game? Please don't.

Create a class with objects of type List


Tag : java , By : Marie Ramos
Date : March 29 2020, 07:55 AM
Related Posts Related QUESTIONS :
  • Print a Winform/visual element
  • C# logic order and compiler behavior
  • When to use an extension method with lambda over LINQtoObjects to filter a collection?
  • How to make a button appear as if it is pressed?
  • C# and Arrow Keys
  • How do you resolve a domain name to an IP address with .NET/C#?
  • Should the folders in a solution match the namespace?
  • How can I evaluate C# code dynamically?
  • CSharpCodeProvider Compilation Performance
  • How can I create Prototype Methods (like JavaScript) in C#.Net?
  • DataTable Loop Performance Comparison
  • CSV string handling
  • What is the best way to do unit testing for ASP.NET 2.0 web pages?
  • High availability
  • What to use for Messaging with C#
  • Accessing a Dictionary.Keys Key through a numeric index
  • ConfigurationManager.AppSettings Performance Concerns
  • What Are Some Good .NET Profilers?
  • Is this a good way to determine OS Architecture?
  • How to create a tree-view preferences dialog type of interface in C#?
  • Searching directories for tons of files?
  • Can I have a method returning IEnumerator<T> and use it in a foreach loop?
  • Why can't I have abstract static methods in C#?
  • Displaying ad content from Respose.WriteFile()/ Response.ContentType
  • Convert integers to written numbers
  • Absolute path back to web-relative path
  • How can we generate getters and setters in Visual Studio?
  • Bringing Window to the Front in C# using Win32 API
  • Possible to "spin off" several GUI threads? (Not halting the system at Application.Run)
  • Reading a C/C++ data structure in C# from a byte array
  • How should I translate from screen space coordinates to image space coordinates in a WinForms PictureBox?
  • Setting Objects to Null/Nothing after use in .NET
  • Converting ARBG to RGB with alpha blending
  • Is it better to create Model classes or stick with generic database utility class?
  • Passing enum type to Converter with integer value
  • Pool of objects with objects that are already on the scene in advance
  • StatusBar text fade-out when binding using Caliburn.Micro
  • Queryfilter on ApplicationUser in OnModelCreating in ApplicationDbContext creates StackOverflowException
  • How to get record form a different table based on a value from first table with linq expression?
  • Show data in Grid from returned model
  • Using Attributes to Override Data Model Conventions
  • Basic OOP console calculator, result Error
  • Compositon and Repository pattern
  • Multiple using statements with if condition
  • How do i increase a number by 1 in every line that contain the number 1
  • Add binding to elements that are created in codebehind
  • How to add a column in an existing AspNetUsers table
  • Order a list of elements with another list of doubles
  • How to setup a NuGet package to copy content files to output build directory?
  • In SignalR Core using ChannelWriter: Do I need to call TryComplete twice if there's an exception?
  • C# GetProcessesByName: issue with colon
  • c# wpf | create complex object with user-defined name to Serialize into JSON
  • How can I get a instance of a generic list with reflection?
  • WPF XAML - Design time and visibility of textbox
  • EF Core and MySql query is too slow
  • Getting Registered App Display Name from an App Id
  • How to get all variables from a string
  • Delete entity with all childs connected
  • Azure Build agent cant´t find class library referance
  • Initialize Nested Dictionaries in c#
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com