Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am just a beginner and I am trying to learn other alternatives.
Is there another way to call a class from another class. For example, I have a class called Test, can you call it in another way from this one = Test example = new Test();
You can have a Type Factory class which can create an instance for you dynamically. Read some articles about type factory.
Its a very common way to dynamically create instances of a type.
Lets say you have a program that generates text files. As parameter you can ask for a specific template, the program should then browse through your classes of type text generator and create an instance matching your requested template.
If you need to create an instance of this class i.e this is not a static class then
Test example = new Test();
is the correct way to call this class.
If this was a static class e.g public static Test {... then you could call this class without creating a new instance of it e.g Test.SomeMethod Example Link
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I was looking for approaches that could be used to access a static class which contains private static methods to unit test.
I came across PrivateObject and PrivateType, I do not understand what the difference between them is, and how you would decided to use either one.
BusinessLogic
public static Class BarLogic(){
private static void FooBar(){
//do something here
}
}
API
public class FooService(){
BarLogic.FooBar();
}
The difference is whether you're accessing otherwise inaccessible instance members or static members.
In the example below, you would use PrivateObject to access _someInstanceField, and PrivateType to access _someStaticField.
public class SomeObjectWithInAccessibleMembers
{
private int _someInstanceField;
private static int _someStaticField;
}
Given that your test was in a separate assembly from this class (and therefore unable to to see its internal members) you could do this:
var privateObject = new PrivateObject(new SomeObjectWithInAccessibleMembers());
privateObject.SetField("_someInstanceField", 1);
Assert.AreEqual(1, privateObject.GetField("_someInstanceField"));
var privateType = new PrivateType(typeof(SomeObjectWithInAccessibleMembers));
privateType.SetStaticField("_someStaticField", 1);
Assert.AreEqual(1, privateType.GetStaticField("_someStaticField"));
I've never used this. I didn't even know about it. My first reaction is that it couples the test to the inner implementation of the class instead of testing its public methods. Then it couples it some more by using strings for member names, so the tests could break just by changing a member name.
On the other hand, I've worked in projects where a) I want to add unit tests, but b) I can't refactor to make it more testable. It's not my first choice but I might have a use for this. Thanks!
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I want to dynamically access classes in c# in the way like Java class.forName(). I have only found things like Class.forName() equivalent in .NET? but I don't want to create instances.
In detail: I have got a simple text file containing a list of classes. I read them using file.ReadLine() (so I have got all class names as strings) and then I want to execute the same static method on each class: class1.method(); class2.method; and so on. The classes all exist and I need to access them. How can I do that?
C# doesn't support static interfaces (or static members in interfaces), so unless you want to use factory classes (the usual approach), you'll need to use reflection to invoke the static method directly.
void Main()
{
Console.WriteLine(Type.GetType("A").GetMethod("Hi").Invoke(null, new object[] {}));
}
class A
{
public static string Hi() { return "Hi!"; }
}
You might want to use a fully-qualified name for the type to make this work really well. Using just the name of the type is tricky, especially when you're trying to invoke types from other assemblies (which you probably are, otherwise there'd be no reason to use reflection - just use a dictionary of delegates or whatever).
You can use System.Reflection to load the Assembly into memory and then drill down to the type followed by getting the required method and invoke.
Refer to the documentation
GetMethod
If you have names of your desired Type then you can use Type.GetType(string) method.
Example if you have a class like this :
namespace MeProgram.BusinessLogic
{
public class MeObject {}
}
Full class name of that object should be "MeProgram.BusinessLogic.MeObject".
Now you can use that name inside of Type.GetType(string) method like such :
string className = "MeProgram.BusinessLogic.MeObject";
Type classType = Type.GetType(className);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I was working with a ModelState.AddModelError() recently. But there is a problem. I have 2 files in one when I type ModelState. intellisense shows the AddModelError() method. But in other file its not. I am getting the error there. ModelState.AddModelError() is defined in the System.Web.Mvc.Controller.The first file contains the Controller class within System.Web.Mvc. But I could not find Controller
class in the second file even though its using System.Web.Mvc. Can anyone help with this?
It doesn't use, it actually inherits from the Controller class. The one in which you could use that must be a Controller which inherits from the Controller parent class.
And the one in which you couldn't use that method must be some other class which obviously didn't inherit from the Controller class.
In order to use some of the functionalities including this, your class has to inherit the parent Controller class.
Hope this clears the idea.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I used to write
Console.WriteLine( "hello World" );
What is this one :
Is WriteLine is a method or a class? ( looks like it is a class)
this.Implementation = () => new Sequence
{
Activities = {
new WriteLine() {
Text = "Hello World" }
}
};
Assuming it compiles...
The first one invokes the WriteLine method of the System.Console class. This is the normal mechanism for writing text output to the console.
The second one is using a class called WriteLine and assigning a value to its Text property. The question is - what is this WriteLine class?
Assuming it's not your own invention, or part of some library you are using, perhaps it is the System.Activities.Statements.WriteLine class. You can check if you have a using statement at the top of your file that imports the System.Activities.Statements namespace. (You would also have had to reference the System.Activities.dll assembly.)
By itself, this will not actually do anything. The resulting class would have to be used in some other way to have any effect.
The System.Activities.Statements.WriteLine class and the assembly it belongs to are part of Windows Workflow Foundation.
See also, the answer to this StackOverflow question, which shows an example usage of the WriteLine class.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Do I always have to use constructor in C#?
I am working on some examples. They use constructor. I am not sure if I understand it, because I feel that I don't have to use constructor.
If a constructor isn't defined, a default constructor is automatically generated for you. The generated code is the same as writing:
public MyClass() : base()
{
}
If you want parameters, or member initialization, then you will need to write your own. You also need to write one if you inherit from a a base class with parameters. Since you are just learning about constructors, you don't need to worry about it yet, but it is something to keep in mind.
Note that this constructor is removed if you define any constructor (even a parameterized one), so you need to explicitly define it if you still want a parameterless one.
Here is the documentation of a default constructor: MSDN
A default constructor is created for every class. However you can create your own constructor with or without parameteres.
You should use Constructors when your class for example needs to manipulate an object that it requires.
For example:
public class Car
{
string _model = "";
public Car(string CarModel)
{
this._model = CarModel;
}
}
Not to be rude but you have to give it a try.
A class does not need a constructor (or at least will generate one calling the base classes' matching constructor, if one) unless:
You want to do some initialisation of variables;
You want to pass in parameters;
You need to since the base class doesn't have a parameterless constructor.
The purpose of constructor is that if you have some code to execute when the object is created, write it in constructor. EDIT Using constructor is not obligatory in C#. For the classes which you create through a wizard, a constructor you will see created by default. But for the class which you create/write, you may create/write constructor.