Get the name of child class inside of super class - puppet

is there a way to get the name of a child class inside of a super class?
class ChildClass inherits SuperClass { }
class SuperClass {
notify { "$NAME_OF_CHILD_CLASS is inheriting me": }
}
The expected output would be ChildClass is inheriting me. I'd need something similar to $module_name which contains the name of the class that contains the current resource's definition.
Is there a way to achieve this? I don't mind if I need a custom function for that...

Related

Singleton service inside a class instance

I have a class in which I need to use a service but this class needs to be instantiated.
It's like this:
class EntrySubject implements ISubject {
constructor(entry: EntryEntity) {}
}
Since I need a service there, I could do this:
class EntrySubject implements ISubject {
constructor(entry: EntryEntity, entryService: EntryService) {}
}
And when using this class inside EntryService I would just instantiate my class as:
const entrySubject = new EntrySubject(entry, this);
But in this case, as far as I understand, every new subject instance would have its own EntryService, but what should I do if I want a single instance of EntryService?
All you have to do is to decorate your EntitySubject, or any other class that you wish to inject, with #Injectable(), and have that class in the 'providers' array of the module. That way, when the constructor has the class in its params, nest will inject a singletone instance of that class.
Please note that when using #Injectable, the default value that is used is #Injectable({scope: DEFAULT}) which means a singletone instance of the class.
For more info on injection scopes visit :
https://docs.nestjs.com/fundamentals/injection-scopes

How to allow class only inherit from one of two interfaces

I have two interfaces Interface1, Interface2. I only want to MyClass inherits from one of them.
It should compile error if I define a class like this:
class MyClass : Interface1, Interface2
{
}
Thanks!
First, it is not inheritance , it is implementation.
you should write one of them
class MyClass: Interface1
{
}

How to find the parent object from the child(property) object in C#?

I have a situation where the child to be able to reference the parent. The reason for this is that I want the child to have the ability to update the parent object.
configuration.Range.Next(configuration)
I do not want to pass the parent object reference instead the Range object should be able to find its parent object. How would I do this?
Class Range { ....MethodX(){How do I access the Configuration Object here } }
Class Configuration { public Range range{get;set;} ..... }
Part of the difficulty with answering your question is that people use the terms "child" and "parent" to mean different things to different people.
One of the most common uses of the terms is as synonyms for subclass (child) and superclass (parent) in an inheritance structure. Assuming that is your meaning, you already have access to anything from the superclass (i.e. "parent") that is declared as public or protected. For example:
public class Parent
{
protected int Foo { get; set; }
}
public class Child : Parent
{
public void DoSomething()
{
Foo = 42;
// or
base.Foo = 42;
}
}
If this isn't the situation you're working with please add more information to your original question to better describe what you mean when you use the terms "child" and "parent."

Groovy: How to get Type on which a static method is called?

When a static method defined on Base class is called using Child class, How to find it was called on Child class type?:
class Base {
static def method(){
println "class on which this method is called? ${this}"
}
}
class Child extends Base {}
Child.method()
In the code above, this rightly points to the Base class.
I don't think it can be done with actual static methods, but a nice alternative is to use the groovy expandoMetaClass to add a static closure method. Inside said closure, you can access the calling class as delegate. I.e.
Base.metaClass.static.anotherMethod = {
println "class on which another method is called? ${delegate}"
}
Calling now Base.anotherMethod() will have delegate referring to Base and calling Child.anotherMethod will have it pointing to Child.

Is it possible in .net to declare variable by class string name, but no direct class name?

I want a magic to happen like that...
class SomeClass {}
public static void main()
MagicFunctionOrMacrosOrSomethingThatGetTypeOf("SomeClass") some_var = null;<br />
}
Maybe it's crazy, but is it possible?
I need it to avoid crazy large switch case, because has a lot of classes, but at certain point of code is unable to tell what of the classes to create.
Let's have an example:
I have base class:
class baseCLASS {}
have a lot of child classes:
class class1 : baseCLASS {}
class class2 : baseCLASS {}
...
class classn : baseCLASS {}
have some flag that comes from outside to events handler:
string class_name; //actually i use int flag that should have connected with string names in dictionary.
have forced to make this:
switch(class_name)
case "class1":
class1 some_class = new class1();
some_class.RunHandler();
break;
case "class2":
class2 some_class = new class2();
some_class.RunHandler();
break;
case "classn":
classn some_class = new classn();
some_class.RunHandler();
break;
}
I want to replace that large switch statement by something like this:
MagicFunctionOrMacrosOrSomethingThatGetTypeOf(class_name) some_var = null;
... some initialization actions with some_var
some_var.Build();
some_var.RunHandler();
oh my...
You have two problems:
Given user input, you want to instantiate an object of a varying class. For this, you want a mapping between user input and .NET type name. For the first: you can use a convention -- ensure that the type name matches the input; or you can use a Dictionary<string,type>, and populate it; or you can attach an attribute to each class, showing which "verb" it handles. Once you've done that, you can use Activator.CreateInstance to (at runtime) create an instance of your unknown class. This returns object.
Calling methods on the unknown object. Either define a base class or common interface for the classes (e.g. IRunnable or something), or use dynamic, which gives you duck typing.

Resources