I have a solution contains 2 projects
windows form application -> WFA
library of user controls -> LUC
Solution
|-Solution.Presentation -> WFA
|-Solution.Controls -> LUC
In LUC I have a control with a property that must show the host project assembly (here my problem is that I do not know how to do it).
public class customControl : TextBox
{
public string HostAssemblyName
{
get { /* HELP => should return Solution.Presentation (must be generated dynamically because the name of the project where it will be hosted is not known) */ }
}
}
The WFA project makes use of the LUC project:
Solutiono
|-Solution.Presentation
|-References
|-Solution.Controls
Any ideas?
Thanks
Related
I am creating a custom C# Windows Forms control library (a DLL) in Visual Studio 2019 (Professional). My control has a property that takes the following form (this property is aimed to be accessed by applications which use the DLL):
public double Hello
{
get { throw new ApplicationException("Hi!"); }
}
(In my efforts to find out why this is happening, I've simplified the property to just throw an exception and do nothing else.)
For some reason, if I run my User Control (in Debug mode), the exception is raised - even though nowhere else in this code calls that property! (The IDE confirms this - saying "0 references" above it). Why does the property "get" accessor seem to be called for no reason? The stack trace shows that the "get" was called by "[External code]"...
This should be pretty easy to reproduce if you have Visual Studio 2019: create a new "Windows Forms Control Library (.NET Framework)" project under C#, then right click on "UserControl1.cs" in the Solution Explorer and click "View Code", then just add in the above code to the class.
I have reproduced your problem. Based on my test, I find that winformscontrollibary will
load all the properties you set in the code, because it needs to load them into the
property bar of form.
Like the following, if you write the following code.
public partial class UserControl1: UserControl
{
public UserControl1()
{
InitializeComponent();
}
public double Hello
{
get { return 1.02; }
}
public int Number
{
get { return 1; }
}
}
You will see the correct property(Hello and Number) in the right of form.
Therefore, if you write the code throw new ApplicationException("Hi!"); in the get method , it will throw the exception.
I have a libary which needs to behave differently for console applications, desktop application (e.g. WPF), and for UWP apps.
How can I determine at run-time into which application type my libary is loaded?
Determining if it is a console application seems easy: How to tell if there is a console
For UWP, I can probably determine if WinRT is loaded. But how?
What distinguishing attributes do desktop applications have?
I ended up defining following enum:
public enum ExecutionMode
{
Console,
Desktop,
UniversalWindowsPlatform
}
which is passed to the constructor of the main class of my libary. Not a new idea, but very reliable (if used correctly).
Create a CustomAttribute in an assembly that is available to all of the applications like so
using System;
namespace MyNamespace.Reflection {
[System.AttributeUsage(AttributeTargets.Assembly)]
public class ApplicationTypeAttribute : Attribute {
public enum ApplicationTypes {
Console,
Desktop,
UWP,
ClassLibrary
}
public ApplicationTypeAttribute(ApplicationTypes appType) {
ApplicationType = appType;
}
public ApplicationTypes ApplicationType { get; private set; } = ApplicationTypes.Console;
}
}
Then add this attribute to your AssemblyInfo.cs file for a console application
using MyNamespace.Reflection;
[assembly: ApplicationType(ApplicationTypeAttribute.ApplicationTypes.Console)]
or a Desktop application
[assembly: ApplicationType(ApplicationTypeAttribute.ApplicationTypes.Desktop)]
etc.
Then wherever you want to check the calling type of the application that was started, use this
using MyNamespace.Reflection;
var assy = System.Relection.Assembly.GetEntryAssembly();
var typeAttribute = assy.GetCustomAttribute(typeof(ApplicationTypeAttribute));
if (typeAttribute != null) {
var appType = ((ApplicationTypeAttribute)typeAttribute).ApplicationType;
}
There is one caveat to this method. .NET Core apps have a different project structure and the AssemblyInfo.cs file is auto-generated at build time by default. You can override this behavior by specifying the following in the .csproj file in the Project node.
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
To match the old project file structure, you can create a Properties directory in the project directtory and then you can add an AssemblyInfo.cs file to that directory. Otherwise you can place the Custom Attribute definition in any file (after the usings and before the namespace declaration).
I am rewriting the Betfair API to JSON from SOAP and I have started off the way I did it before as a console APP which is then called from a task scheduler or win service.
However now I have been asked to do various different jobs with the code and I don't want to write a console app for each job (different sites want prices, bets placed etc)
The new codebase is much larger than the old one and I would have been able to copy the 4 files from the old system into a DLL app and then create various console apps/services to implement the DLL - however because it's 40+ files I don't want a copy n paste job if possible.
Is there a way I can EASILY convert an existing console project into a class / DLL project with some tool or command in VS?
I want to be able to just then create simple apps that just go
BetfairBOT myBOT = new BetfairBOT()
myBOT.RunGetPrices();
or
BetfairBOT myBOT = new BetfairBOT()
myBOT.RunPlaceBets();
e.g 2/3 lines of code to implement my DLL that is registered to my app.
So without copy and paste can I do this.
I am using VS 2012, .NET 4.5 (or 4.0 if I need to depending on server), Windows 8.1
Any help would be much appreciated.
This answer is from here. while it used winforms instead of console application, I think you will be able to use it.
Steps for creating DLL
Step 1:- File->New->Project->Visual C# Projects->Class Library. Select your project name and appropriate directory click OK
After Clicking on button ‘OK’, solution explorer adds one C# class ‘Class1.cs’. In this class we can write our code.
When we double click on Class1.cs, we see a namespace CreatingDLL. We will be use this namespace in our project to access this class library.
Step 2:- Within Class1.cs we create a method named ‘sum’ that takes two integers value and return sum to witch method passed numbers.
using System;
namespace CreatingDLL
{
public class Class1
{
/// <summary>
/// sum is method that take two integer value and return that sum
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public int sum(int x, int y)
{
return x + y;
}
}
}
Step 3:- Now build the Application and see bin\debug directory of our project. ‘CreatingDLL.dll’ is created.
Now we create another application and take this DLL (CreatingDLL.dll) reference for accessing DLL’s method.
Steps for accessing created DLL
Step 4:- File->New->Project->Visual C# Projects->Windows Form Application.
Step 5:- Designed windows form as bellow figure.
Step 6:- Add reference of DLL (CreatingDLL) which we created before few minutes.
After adding reference of DLL, following windows will appear.
Step 7:- Write code on button click of Windows Form Application. Before creating object and making method of Add DLL, add namespace CreatedDLL in project as bellow code.
using System;
using System.Windows.Forms;
using CreatingDLL;
namespace AccessingDLL
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnAdd_Click(object sender, EventArgs e)
{
Class1 c1 = new Class1();
try
{
txtResult.Text = Convert.ToString(c1.sum(Convert.ToInt32(txtNumber1.Text), Convert.ToInt32(txtNumber2.Text)));
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
Step 8:- Now build the application and execute project and see output.
Edit: To change an application into a library do these steps
First, double click on Properties inside Solution Explorer window.
Then, On the openned page, change the Output Type from Console Application to Class Library
I have been trying to communicate with my Unity C# script through my Android Application using
"UnityPlayer.UnitySendMessage("Cube", "Test","HELLO") - where "Cube" is my Unity Object, "Test" is the name of the method present in the C# script it is using and "HELLO" is the message String I want to pass.
This line is placed in my onClick function, like this:
ImageButton right_button = (ImageButton) findViewById(R.id.right_arrow);
right_button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
UnityPlayer.UnitySendMessage("Cube", "Test","HELLO");
}
});
But when I run my android application and as soon as I click this button, I get this error:
09-11 14:22:37.526: E/AndroidRuntime(1330): java.lang.NoClassDefFoundError: com.unity3d.player.UnityPlayer
I have included classes.jar in my build path also.
Is there anything else I am missing out?
Thanks in advance!
Check if the class.jar file is in the Build Path of your Project. And if so, check if it is selected (check box is marked).
Tip: if you get ClassDefNotFound runtime errors (or similar), you might need to perform this extra step:
go to Project > Properties > Java Build Path -> Order and Export tab and check (tick) the classes.jar, Android x.y and Android Dependencies items; then Apply and rebuild the probject.
Happened to me the other time too.
I clean, re-link and rebuild my eclipse project. Re-Export my jar file to unity and also restart Unity. Build again and the error is gone.
I have two project and having trouble on passing value between two project. As usual I have passed the file reference between project.
My project Details Is:
Project1 Project2
All forms and object Only 1 MDI Forms Containing ManuStrip
I wants to read the data of MDI Forms on showing the project1 forms
The Example is as below:
//This is on Project2 MDI Forms
private void accountMasterToolStripMenuItem_Click(object sender, EventArgs e)
{
INVOICE1.Form24 f24 = new INVOICE1.Form24();
f24.PFrom.Text = label4.Text;
f24.PTo.Text = label5.Text;
f24.Namee.Text = textBox1.Text;
f24.ID.Text = label6.Text;
f24.ShowDialog();
}
I Have Created the Properties for the same on project1 forms
public Label PFrom
{
get { return label14; }
set { label14 = value; }
}
public Label PTo
{
get { return label16; }
set { label16 = value; }
}
public Label Namee
{
get { return label2; }
set { label2 = value; }
}
public Label ID
{
get { return label3; }
set { label3 = value; }
}
The value passed from MDI To Project1 is not showing on Form24 of Project1. There are no error. The Form24 Showing without value which are passed from MDI Form.
Why The value not showing on form24 of project1 ?. And What is the Solution?.
You may have forgotten to add a project reference to Project1 in Project2. In the Solution Explorer, right-click Project2 and select "Add Reference", then under "Projects" select Project1.
Also, if the two projects have different namespaces, you'll need to put
using Project1; // replace "Project1" with the namespace of your Project1
At the top of the Project2 source file.
If there are no compiler errors then the problem is not likely to be with project references. Perhaps you have some code in the Form24 constructor or Load event which is clearing those labels
As a side note, instead of exposing the Labels as properties, just expose their Text property:
public string PFrom
{
get { return label14.Text; }
set { label14.Text = value; }
}
I have faced with the same problem..and answer was simple: it is impossible. However you can crack this situation...using database or shared solution in which you will establish communication between 2 projects. Or use 3 project and create communication driver, which will be used in 1 and 2 project. It does't meter how you will do this.
situation: database
situation: shared solution with communication protocol
perhaps creating DLL will also help
My personal solution was this: (Tested on real website and separate background project)
To create shared project, use VS template (shared project). Then create your class and inside each project include in project reference section your shared project. So for example Project A, Project B , Project SharedPr -> contains communication protocol
Project A -> references-> Add Reference -> Shared Project.
Project B -> references-> Add Reference -> Shared Project.
That communication protocol it is something like a driver which you need to create.
Easily to do this by FILE. Create Hidden file in which project A writes and project B reads. Store in your file json string or json array then read all lines and deserialize everything for example with Newtonsoft.Json NuGet package.
I hope this will help.