运行时编译的类不能实现接口- Roslyn
本文关键字:接口 Roslyn 实现 不能 编译 运行时 | 更新日期: 2023-09-27 18:03:29
我使用2012年9月的Roslyn。我试图从文件创建一个类的实例。问题是类正在实现和重写一些方法。当我尝试运行运行时编译时,会得到如下错误:
error CS0122: 'GameObject' is inaccessible due to its protection level Roslyn.Compilers.CSharp.Diagnostic
error CS0115: 'rotate(float)': no suitable method found to override Roslyn.Compilers.CSharp.Diagnostic
看起来类没有看到接口和覆盖函数。但是我的编译看起来:
var comp = Compilation.Create("Test.dll"
, syntaxTrees: new[] { syntaxTree }
, references: new[] {
MetadataFileReference.CreateAssemblyReference("mscorlib"),
new MetadataFileReference(typeof(Game.IGameObjectInterface).Assembly.Location),
new MetadataFileReference(typeof(Team).Assembly.Location),
new MetadataFileReference(typeof(System.Linq.Enumerable).Assembly.Location),
new MetadataFileReference(typeof(LinkedList<>).Assembly.Location),
}
, options: new CompilationOptions(OutputKind.DynamicallyLinkedLibrary)
);
添加接口上的引用。
我做错了什么?
编辑GameObjectabstract class GameObject: IGameObject {
protected string name;
protected Entity entity;
.
.
.
//Look here create file load file
static GameObject() {
gameActionsPermitions = new Dictionary<string, List<IStaticGameObject>>();
gameActions = new Dictionary<string, IGameAction>();
IGameAction o = (IGameAction)System.Reflection.Assembly.GetExecutingAssembly().CreateInstance("Strategy.GroupControl.Game_Objects.GameActions.Produce");
gameActions.Add(o.getName(), o);
gameActionsPermitions.Add(o.getName(), new List<IStaticGameObject>());
}
.
.
.
public abstract void rotate(float f);
public abstract void nonActiveRotate(float f);
protected abstract void onDisplayed();
.
.
.
}
And interface IGameObject
interface IGameObject{
void rotate(float f);
void nonActiveRotate(float f);
void changeVisible(bool visible);
string getName();
string getMesh();
bool tryExecute(string executingAction);
Team Team { get; set; }
}
和函数从文件是
public override void rotate(float f) {
tryExecute("Produce");
sceneNode.Roll(new Mogre.Degree((float)(mFlySpeed * 0.5 *f)));
//position in LinkedList now moving
if (!mFlying) {
if (nextLocation()) {
mFlying = true;
mDestination = circularPositions.First.Value; //get the next destination.
prepareNextPosition();
//update the direction and the distance
mDirection = mDestination - sceneNode.Position;
mDistance = mDirection.Normalise();
} else {
}//nothing to do so stay in position
} else {
double move = mFlySpeed * f;
mDistance -= move;
if (mDistance <= .0f) { //reach destination
travelledInvisible = 0;
sceneNode.Position = mDestination;
mDirection = Mogre.Vector3.ZERO;
mFlying = false;
} else {
sceneNode.Translate(mDirection * (float)move);
}
}
}
EDIT2:
类 的文件using System;
using System.Collections.Generic;
using System.Linq;
using Mogre;
using Strategy.TeamControl;
namespace Strategy.GroupControl.Game_Objects.StaticGameObjectBox {
class Planet : GameObject {
protected double mDistance = 0.0f; //distance to positoin
protected Mogre.Vector3 mDirection = Mogre.Vector3.ZERO; // The direction the object is moving
protected bool mFlying = false; //bool to detect if object walking or stay
protected double mFlySpeed = 200f; //speed of planet
protected double travelledInvisible;
private static Random random = new Random();
///testing
public Planet() {
}
public Planet(string s) {
name = s;
}
//end
public Planet(string name, string mesh, Team myTeam, Mogre.SceneManager manager, double distanceFromCenter,
Vector3 center, int circularNum = 30) {
this.name = name;
this.mesh = mesh;
planetTeam = myTeam;
this.manager = manager;
//prepare list of positions
circularPositions = calculatePositions(circularNum, distanceFromCenter,center);
randomizeStartPosition(circularNum); // randomize start position
mDestination = circularPositions.First();
//Mogre inicialization of object
entity = manager.CreateEntity(name, mesh);
}
/// <summary>
/// Rotating in visible mood, it means when planet is in active solar system
/// </summary>
/// <param name="f">delay between frames</param>
public override void rotate(float f) {
tryExecute("Produce");
sceneNode.Roll(new Mogre.Degree((float)(mFlySpeed * 0.5 *f)));
//position in LinkedList now moving
if (!mFlying) {
if (nextLocation()) {
mFlying = true;
mDestination = circularPositions.First.Value; //get the next destination.
prepareNextPosition();
//update the direction and the distance
mDirection = mDestination - sceneNode.Position;
mDistance = mDirection.Normalise();
} else {
}//nothing to do so stay in position
} else {
double move = mFlySpeed * f;
mDistance -= move;
if (mDistance <= .0f) { //reach destination
travelledInvisible = 0;
sceneNode.Position = mDestination;
mDirection = Mogre.Vector3.ZERO;
mFlying = false;
} else {
sceneNode.Translate(mDirection * (float)move);
}
}
}
/// <summary>
/// Function calculate moves in invisible mode
/// </summary>
/// <param name="f">delay between frames</param>
public override void nonActiveRotate(float f) {
tryExecute("Produce");
if (!mFlying) {
if (nextLocation()) {
mFlying = true;
mDestination = circularPositions.First.Value; //get the next destination.
prepareNextPosition();
mDistance = mDirection.Normalise();
} else {
}//nothing to do so stay in position
} else {
double move = mFlySpeed * f;
mDistance -= move;
if (mDistance <= .0f) { //reach destination
travelledInvisible = 0;
mDirection = Mogre.Vector3.ZERO;
mFlying = false;
} else {
travelledInvisible += move;
}
}
}
//own functions
/// <summary>
/// Randomize starting position of planet
/// </summary>
/// <param name="max">max of rotates</param>
private void randomizeStartPosition(int max) {
for (int i = 0; i < getRandomNumber(max); i++) {
prepareNextPosition();
}
}
private static int getRandomNumber(int max) {
return random.Next(max);
}
/// <summary>
/// Cyclic remove from LinkedList and add on the end
/// </summary>
private void prepareNextPosition() {
var tmp = circularPositions.First; //save the node that held it
circularPositions.RemoveFirst(); //remove that node from the front of the list
circularPositions.AddLast(tmp); //add it to the back of the list.
}
/// <summary>
/// Calculate posistion on circle represent as ngon
/// </summary>
/// <param name="circularNum">Number of positions on circle</param>
/// <param name="distanceFromCenter">radius on circle</param>
/// <returns>linkedList with position on ngon (circle)</returns>
private LinkedList<Mogre.Vector3> calculatePositions(int circularNum, double distanceFromCenter,Vector3 center) {
var list = new LinkedList<Mogre.Vector3>();
for (int i = 0; i < circularNum; i++) {
double x = System.Math.Cos(i * 2 * System.Math.PI / circularNum) * distanceFromCenter;
double y = System.Math.Sin(i * 2 * System.Math.PI / circularNum) * distanceFromCenter;
list.AddFirst(new Mogre.Vector3((float)x + center.x, 0, (float)y)+ center.y);
}
return list;
}
/// <summary>
/// The NextLocation() check if exist next location to move
/// </summary>
/// <returns>true ->exist / false -> not exist</returns>
private bool nextLocation() {
if (circularPositions.Count == 0) {
return false;
}
return true;
}
/// <summary>
/// Called when object is displayed (invisible to visible)
/// </summary>
protected override void onDisplayed() {
sceneNode.Position = mDestination;
mFlying = false; //jump correction
}
}}
这些看起来像是标准的编译器错误。
我怀疑你的GameObject
类没有声明为public
。这将导致它对你通过Roslyn创建的程序集不可见,因为它正在编译一个新的(独立的)程序集,而不是定义你的类型的程序集。