我在autocad 2019 64位中的visual studio 2022中测试了C#10,但并非所有代码都工作正常。在我的代码的最后一部分中的数组索引示例
请参见快照
如何使所有代码在C#10中正常工作
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Autodesk.AutoCAD.ApplicationServices.Core;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.Geometry;
- using Autodesk.AutoCAD.Runtime;
- using Autodesk.AutoCAD.EditorInput;
- using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
- using Autodesk.AutoCAD.DatabaseServices.Filters;
- using System.Collections;
- using static cadtest22_1.PersonClass;
- namespace cadtest22_1
- {
-
- //1.test create struct Tipe T
- public struct Foo
- {
- public T Var1;
- public T Var2;
- }
- public struct Point
- {
- public double X { get; set; }
- public double Y { get; set; }
- public double Distance => Math.Sqrt(X * X + Y * Y);
- public override string ToString() =>
- $"({X}, {Y}) is {Distance} from the origin";
- }
-
- //2. test record
- public class PersonClass
- {
- public string Name { get; }
- public string Surname { get; set; }
- public record PersonRecord
- {
- public string Name { get; set; }
- public string Surname { get; set; }
- public PersonRecord(string name, string surname)
- {
- Name = name;
- Surname = surname;
- }
- public void Deconstruct(out string name, out string surname)
- {
- name = Name;
- surname = Surname;
- }
- }
- }
- public class Class1
- {
-
- //3.test call
- int M()
- {
- int y = 5;
- int x = 7;
- return Add(x, y);
- static int Add(int left, int right) => left + right;
- }
-
- [CommandMethod("test")]
- public void test1()
- {
- var doc = CadApp.DocumentManager.MdiActiveDocument;
- var ed = doc.Editor;
- var db = doc.Database;
- //call struct foo
- var d = new Foo[i] { Var1 = 1, Var2 = 2 }; //ok
- var dd = new Foo { Var1 = 1.1, Var2 = 20.1 }; //ok
- CadApp.ShowAlertDialog($"struct foo T {d.Var1} - {d.Var2}"); //ok
- // test switch multiple variable
- string RockPaperScissors(string first, string second)
- => (first, second) switch
- {
- ("rock", "paper") => "rock is covered by paper. Paper wins.",
- ("rock", "scissors") => "rock breaks scissors. Rock wins.",
- ("paper", "rock") => "paper covers rock. Paper wins.",
- ("paper", "scissors") => "paper is cut by scissors. Scissors wins.",
- ("scissors", "rock") => "scissors is broken by rock. Rock wins.",
- ("scissors", "paper") => "scissors cuts paper. Scissors wins.",
- (_, _) => "tie"
- };
- CadApp.ShowAlertDialog($"{RockPaperScissors("rock","paper")}");
- //add function without aggregate,action
- int M()
- {
- int y = 5;
- int x = 7;
- return Add(x, y);
- static int Add(int left, int right) => left + right;
- } //ok
- //test multiple switch
- static int GetTax4(int muncipalityId) => muncipalityId switch
- {
- 0 or 1 => 20,
- > 1 and 21,
- > 5 and not 7 => 22,
- 7 => 23,
- _ => 20
- };
- CadApp.ShowAlertDialog("value switch " + GetTax4(6).ToString());//ok
- // test switch to autocad
- static string ent(Entity myent) => myent.GetType().Name.ToString() switch
- {
- "Line" or "Polyline" or "Circle" => "curve",
- _ => "Not curve"
- };
- var ge = ed.GetEntity("\nclick entiti");
- if (ge.Status != PromptStatus.OK) return;
- using(var tr = db.TransactionManager.StartTransaction())
- {
- var ent1 = tr.GetObject(ge.ObjectId, OpenMode.ForRead) as Entity;
|