How can refer base class instance? - c#-2.0

The following example, how do I refer base class instance?
public class A
{
public string test;
public A()
{
B b = new B();
test = "I am A class of test.";
}
public void hello()
{
MessageBox.Show("I am A class of hello.");
}
class B
{
public B()
{
//Here...
//How can I get A class of test and call A class of hello method
//base.test or base.hello() are not working.
}
}
}

You'd have to pass a reference of A to B.
One way you can do this is as follows:
public class A
{
string name = "Class A";
public A()
{
var b = new B(this);
}
class B
{
public B(A a)
{
a.name.Dump(); // Write out the property of a.name to some stream.
}
}
}

To clearly distinguish between base class and nested class, please refer the example below.
namespace Example
{
class A
{
string Name = "test"; // access restricted only to this class
public string Type; // global access
internal string Access; // within defining namespace
protected string Code; // this class and subclass
// When you create a nested class like C, you can create instances of C within this class(A).
C c = new C();
class C
{
string name;
public C()
{
//this is a nested class and you cannot call A as its base
name = "test success";
}
}
}
class B : A
{
public string Type { get { return base.Type; } set { base.Type = value; } } // You can use base when you hide a base class member
public B()
{
Type = "test";
Code = "nothing";
Access = "success";
//Cannot Access 'Name' Here as it is private
}
}
}

Related

Google Guice binding using Annotation and Key class

Lets say we have A.java interface implemented by AImpl.java and B.java implemented by Bimpl.java
Above classes are binded in two modules as below
Module1 {
bind(A.class).to(AImpl.class);
bind(B.class).to(BImpl.class);
}
Module2 {
Key<A> aKey = Key.get(A.class, AnAnnot.class);
bind(aKey).to(AImpl.class);
Key<B> bKey = Key.get(B.class, AnAnnot.class);
bind(bKey).to(BImpl.class);
}
Class AImpl implements A {
}
Class BImpl implements B {
#Inject
BImpl(A aImpl) {
//??
}
}
BImpl refers to A
For BImpl binded using Annotation, I want corresponding aImpl, binded using Annotation but here I'm getting aImpl which is not binded using Annotation
Please suggest
I'm able to achieve using below pattern. May be there is a more easier way. Happy to know more
A.java
public interface A {
String aMethod();
}
AImpl.java
public class AImpl implements A {
private String moduleName;
public AImpl(String moduleName) {
this.moduleName = moduleName;
}
#Override
public String aMethod() {
return moduleName;
}
}
B.java
public interface B {
String bMethod();
}
Bimpl.java
public class BImpl implements B {
private final A a;
BImpl(A a) {
this.a = a;
}
#Override
public String bMethod() {
return a.aMethod();
}
}
AnAnnot.java
#Target(PARAMETER)
#Retention(RUNTIME)
#BindingAnnotation
public #interface AnAnnot {
}
BProvider.java
public class BProvider implements Provider<B> {
private final A a;
#Inject
BProvider(A a) {
this.a = a;
}
#Override
public B get() {
return new BImpl(a);
}
}
BHavingAnnotatedA.java
public class BHavingAnnotatedA implements Provider<B> {
private final A a;
#Inject
BHavingAnnotatedA(#AnAnnot A a) {
this.a = a;
}
#Override
public B get() {
return new BImpl(a);
}
}
ABModule1.java
public class ABModule1 extends AbstractModule {
#Override
protected void configure() {
bind(A.class).to(AImpl.class);
bind(B.class).toProvider(BProvider.class);
}
}
ABModule2.java
public class ABModule2 extends AbstractModule {
#Override
protected void configure() {
Key<A> aKey = Key.get(A.class, AnAnnot.class);
bind(aKey).to(AImpl.class);
Key<B> bKey = Key.get(B.class, AnAnnot.class);
bind(bKey).toProvider(BHavingAnnotatedA.class);
}
}

Get class name of class

Groovy beginner, coming from Java / Kotlin, how do I get the class name of an (anonymous) implementation of my class?
Failed attempts:
abstract class Foo {
String name() { this.class.simpleName }
}
abstract class Foo {
String name() { return this.class.simpleName }
}
abstract class Foo {
String name() { return getClass().getSimpleName() }
}
abstract class Foo {
String name() { this.metaClass.classNode.nameWithoutPackage }
}
So obviously I seem to give some class instance a default name. I thought "well, if class name does not work, let's try individual naming" with this:
abstract class Foo {
private final AtomicInteger counter = new AtomicInteger(0)
String name() { "number " + this.counter.incrementAndGet() }
}
but this doesn't work either because counter is not a property of groovy.lang.Binding.
This is all in context of a Jenkins pipeline I try to write... why is this so hard?
You probably want this...
abstract class Foo {
String name() { this.class.name }
}

Autofac. How to get caller class Type?

Suppose we have two classes with same constructor Injectable dependency:
public class FirstClass
{
public FirstClass(ISomeDependency someDependency)
{ }
}
public class SecondClass
{
public SecondClass(ISomeDependency someDependency)
{ }
}
Now we have a registration for ISomeDependency:
builder.Register(x =>
{
string key = GetKeyFromCurrentHttpRequest();
// if "Caller" is "FirstClass" return new Dependency(key);
// else return new Dependency("defaultKey");
}).As<ISomeDependency>();
Note: This is a simplified use case. The real scenario is much more complicated.
1. How to get "Caller" type which tryies to resolve ISomeDependency?
2. Is there a better way design for such situations?
You can use delegate factories do achieve your goal. The only drawback is the FirstClass and SecondClass cannot use ISomeDependency as parameter.
You can try this code in a console application (just add Autofac dependency).
using System;
using Autofac;
namespace test
{
class MainClass
{
public static void Main(string[] args)
{
ContainerBuilder builder = new ContainerBuilder ();
builder.RegisterType<SomeDependency>().As<ISomeDependency>();
builder.RegisterType<FirstClass>();
builder.RegisterType<SecondClass>();
var container = builder.Build();
var dummy = container.Resolve<FirstClass>();
var dummy2 = container.Resolve<SecondClass>();
}
public interface ISomeDependency
{
}
public class SomeDependency : ISomeDependency
{
public delegate ISomeDependency Factory(string value);
private readonly string _value;
public SomeDependency(string value)
{
_value = value;
Console.WriteLine("Value = " + _value);
}
}
public class FirstClass
{
private ISomeDependency _dependency;
public FirstClass(SomeDependency.Factory factory)
{
_dependency = factory.Invoke("my value");
}
}
public class SecondClass
{
private ISomeDependency _dependency;
public SecondClass(SomeDependency.Factory factory)
{
_dependency = factory.Invoke("my value 2");
}
}
}
}

Dagger - Is it possible to select a Provider based on inheritance?

At the moment I have a Base class that contains a member I would like to inject. However, I would like the concrete type of this member to depend on the Subclass being instantiated. What I am aiming for is something along these lines:
public interface StringInterface {
public String getString();
}
public class HelloStringConcrete implements StringInterface {
public String getString() {
return "Hello";
}
}
public class WorldStringConcrete implements StringInterface {
public String getString() {
return "World";
}
}
public abstract class Base {
#Inject StringInterface member;
public Base() {
// Assume access to object graph
MyObjectGraph.get().inject(this);
}
public void printSomething() {
System.out.println(member.getString());
}
}
public class SubclassHello extends Base {}
public class SubclassWorld extends Base {}
#Module(injects = {SubclassHello.class})
public class HelloModule {
#Provides StringInterface provideStringInterface() {
return new HelloStringConcrete();
}
}
#Module(injects = {SubclassWorld.class})
public class WorldModule {
#Provides StringInterface provideStringInterface() {
return new WorldStringConcrete();
}
}
So now what I would like to do is something along the lines of:
#Module(
includes = {
HelloModule.class,
WorldModule.class
}
)
public class BigModule {}
// Somewhere in another piece of code...
objectGraph = ObjectGraph.create(new BigModule());
// In yet another piece of code...
SubclassHello hello = new SubclassHello();
SubclassWorld world = new SubclassWorld();
hello.printSomething();
world.printSomething();
// Hopefully would result in :
// Hello
// World
This type of setup won't work though, because including two modules with the same provider will result in a duplicate provider error at compile time. It would be cool to see a solution to this problem without introducing #Named or #Qualifer annotations, or using scoped graph extensions via graph.plus() because these strategies necessarily introduce coupling to the subclasses
This is possible but I think the code I've attached below is more coupled than using scoped graphs or annotations. Basically you can use constructor injection to inject concrete dependencies to your
SubclassHello and SubclassWorld.
public abstract class Base {
private final StringInterface member;
public Base(StringInterface member) {
this.member = member;
}
...
}
#Module(injects = {SubclassWorld.class})
public class WorldModule {
#Provides
WorldStringConcrete provideStringInterface() {
return new WorldStringConcrete();
}
}
public class SubclassWorld extends Base {
#Inject
public SubclassWorld(WorldStringConcrete worldStringConcrete) {
super(worldStringConcrete);
}
}
#Module(injects = {SubclassHello.class})
public class HelloModule {
#Provides
HelloStringConcrete provideStringInterface() {
return new HelloStringConcrete();
}
}
public class SubclassHello extends Base {
#Inject
public SubclassHello(HelloStringConcrete helloStringConcrete) {
super(helloStringConcrete);
}
}
// Somewhere in another piece of code...
ObjectGraph objectGraph = ObjectGraph.create(new BigModule());
// In yet another piece of code...
SubclassHello hello = objectGraph.get(SubclassHello.class);
SubclassWorld world = objectGraph.get(SubclassWorld.class);
I don't think there are other solutions. How could Dagger find out which StringInterface implementations should be injected to the concrete classes?

Other Classes in Class arguments

How can I set another Class as a Class constructor argument?
class MyClass {
String message = 'myClass';
void print() {
print(this.message);
}
}
class TestClass {
var myClass;
TestClass(???) {
this.myClass = ???(); // ???
}
void debug() {
print('DEBUG: ');
this.myClass.print();
}
}
main() {
MyClass myClass;
testClass(myClass);
}
You can't (at least not now). Your best option is to pass a builder function into the constructor.
class MyClass {
var clazzBuilder;
MyClass(this.clazzBuilder);
doStuff() {
var instance = clazzBuilder();
:
}
}
and then use it like this
var myTest = new MyClass(() => new Test());
var myProd = new MyClass(() => new Prod());
Note that the Dart team is planning to add both class concept (.type) and reflection to the language.
What do you want actually do? Often people try to apply paradigms from some languages in some other languages which isn't the best way to do some things.
Since print is an instance method and not a static method, you don't need to pass a class itself to TestClass, just an instance of it. Your program can be fixed like so:
class MyClass {
String message = 'myClass';
void print() {
print(this.message);
}
}
class TestClass {
var myClass;
TestClass(MyClass myClass) {
this.myClass = myClass;
}
void debug() {
print('DEBUG: ');
this.myClass.print();
}
}
main() {
MyClass myClass = new MyClass();
testClass(myClass);
}
Or, more idiomatically:
class MyClass {
String message = 'myClass';
void print() => print(message);
}
class TestClass {
var myClass;
TestClass(this.myClass);
void debug() {
print('DEBUG: ');
myClass.print();
}
}
main() {
testClass(new MyClass());
}

Resources